五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

python 開心網(wǎng)和豆瓣日記爬取的小爬蟲

發(fā)布日期:2022-03-31 18:26 | 文章來(lái)源:源碼之家

項(xiàng)目地址:

https://github.com/aturret/python-crawler-exercise

用到了BeautifulSoup4,請(qǐng)先安裝。

pip install beautifulsoup4

開心網(wǎng)日記爬取

kaixin001.py

使用

登錄開心網(wǎng),瀏覽器F12看http請(qǐng)求的header,獲取自己的cookie。

填寫cookie,要爬的日記的url,要爬的總次數(shù)。走你。

之后會(huì)生成HTML文件,格式是<:title>-<YYYYMMDDHHMMSS>

代碼

# -*- coding: utf-8 -*-
from urllib.request import urlopen
import urllib.request
import urllib.parse #為了獲取HTTP response
from bs4 import BeautifulSoup #BS4
import string # 為了去掉空白字符
import time # 防止被殺cookie
import unicodedata # 字符修正
# 在這里放第一個(gè)鏈接
urlx = '鏈接' #寫你想爬的文
def request(url):
 global urlx #引用外面的鏈接作為全局變量,后面還會(huì)取下一個(gè)進(jìn)行循環(huán)的

# 使用urllib庫(kù)提交cookie獲取http響應(yīng)
 headers = {
 'GET https':url,
 'Host':' www.kaixin001.com',
 'Connection':' keep-alive',
 'Upgrade-Insecure-Requests':' 1',
 'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
 'Accept':' application/json, text/javascript, */*; q=0.01',
 'Accept-Language':' zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
 'Cookie':' ', #改成自己的cookie,自己瀏覽器打開網(wǎng)站F12調(diào)試,自己找http請(qǐng)求的header
 }
 request = urllib.request.Request(url=url,headers=headers)
 response = urllib.request.urlopen(request)
 contents = response.read()
# 使用BS4獲得所有HTMLtag
 bsObj = BeautifulSoup(contents,"html.parser")
# 使用BS4的find函數(shù)得到想要的東西:標(biāo)題、發(fā)表時(shí)間和博客正文
 title = bsObj.find("b", attrs={"class":"f14"})
 titleT = bsObj.find("b", attrs={"class":"f14"}).get_text() #開心網(wǎng)日記的標(biāo)題是一個(gè)b標(biāo)簽,class屬性值是f14
 date = bsObj.find("span", attrs={"class":"c6"})
 dateT = bsObj.find("span", attrs={"class":"c6"}).get_text() #開心網(wǎng)日記的發(fā)表時(shí)間是一個(gè)span標(biāo)簽,class屬性值是c6
 text = bsObj.find("div", attrs={"class":"textCont"})
 textT = bsObj.find("div", attrs={"class":"textCont"}).get_text() #開心網(wǎng)日記的正文是一個(gè)div標(biāo)簽,class屬性值是textCont
  
# 測(cè)試輸出
 print(title)
 print(dateT)
 # print(text)
 
 
 
# 生成HTML文件。這里直接用file.open()和file.write()了,也可以用jinja2之類的框架生成。
 remove = string.whitespace+string.punctuation
 table = str.maketrans(':',':',remove)
 fileTitle=str(titleT).replace(':',':').replace('''"''','''“''')+'-'+str(dateT).translate(table).replace('發(fā)表','')+'.html'
 print(fileTitle) #測(cè)試輸出
 f = open(fileTitle,'w',encoding="utf-8") #注意用utf-8編碼寫入,不然會(huì)因?yàn)橐恍┡f博文采用的gbk編碼不兼容而出問題。
# 寫入message
 message = """
 <html>
 <head></head>
 <body>
 <h1>%s</h1>
 <b>%s</b>
 <br></br>
 %s
 </body>
 </html>"""%(title.get_text(),date.get_text(),unicodedata.normalize('NFD',text.prettify()))
 f.write(message)
 f.close()
 # webbrowser.open(fileTitle,new = 1)

# 定位下一篇博文的URL
 nextUrl=bsObj.find("a",text="下一篇 >").attrs["href"] #下一篇是一個(gè)a標(biāo)簽,使用tag對(duì)象的attrs屬性取href屬性的值。開心網(wǎng)的日記系統(tǒng)里,如果到了最后一篇日記,下一篇的鏈接內(nèi)容是第一篇日記,所以不用擔(dān)心從哪篇日記開始爬。
 # print(nextUrl)
 urlx="http://www.kaixin001.com"+nextUrl
 print(urlx)

# 主循環(huán),給爺爬
num=328 #設(shè)定要爬多少次。其實(shí)也可以寫個(gè)數(shù)組檢測(cè)重復(fù)然后中止的啦,但我懶得弄了。
for a in range(num):
 request(urlx) 
 print('We get '+str(a+1)+' in '+str(num))
 time.sleep(1) # 慢點(diǎn),慢點(diǎn)。測(cè)試過程中出現(xiàn)了沒有設(shè)置限制爬一半cookie失效了的情況,可能是太快了被搞了。

豆瓣日記爬取

douban.py

使用

登錄豆瓣,瀏覽器F12看http請(qǐng)求的header,獲取自己的cookie。

填寫變量COOKIE,要爬的日記頁(yè)的url。走你。

之后會(huì)生成HTML文件,格式是<:title>-<YYYYMMDDHHMMSS>

代碼

# -*- coding: utf-8 -*-
from urllib.request import urlopen
import urllib.request
import urllib.parse #為了獲取HTTP response
from bs4 import BeautifulSoup #BS4
import string # 為了去掉空白字符
import unicodedata # 字符修正
import re
# 在這里放鏈接
url = '' #寫你想爬的人 https://www.douban.com/people/xxx/notes 這樣
COOKIE = ''
def request(urlx):
 global url #引用外面的鏈接作為全局變量,后面還會(huì)取下一個(gè)進(jìn)行循環(huán)的
 global boolean
 global COOKIE
# 使用urllib庫(kù)提交cookie獲取http響應(yīng)
 headers = {
 'GET https':urlx,
 'Host':' www.douban.com',
 'Connection':' keep-alive',
 'Upgrade-Insecure-Requests':' 1',
 'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
 'Accept':' application/json, text/javascript, */*; q=0.01',
 'Accept-Language':' zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
 'Cookie':COOKIE, #改成自己的cookie,自己瀏覽器打開網(wǎng)站F12調(diào)試,自己找http請(qǐng)求的header
 }
 request = urllib.request.Request(url=urlx,headers=headers)
 response = urllib.request.urlopen(request)
 contents = response.read()
# 使用BS4獲得所有HTMLtag
 bsObj = BeautifulSoup(contents,"html.parser")
# 使用BS4的find函數(shù)獲取當(dāng)前頁(yè)面的所有日記鏈接
 article = bsObj.find("div", attrs={"class":"article"})
 titleSet = article.findAll("h3")
 # print(titleSet)
 for title in titleSet:
  titleText = title.findAll("a",attrs={"class":"j a_unfolder_n"})
  for link in titleText:
noteUrl = str(link.attrs["href"])
print(noteUrl)
requestSinglePage(noteUrl)
 next = bsObj.find("a",text="后頁(yè)>")
 if next==None:
  print("結(jié)束了")
  boolean=1
 else:
  url = str(next.attrs["href"]).replace("&type=note","")
  print(url)
def requestSinglePage(urly):
 global COOKIE
 headers = {
  'GET https':urly,
  'Host':' www.douban.com',
  'Connection':' keep-alive',
  'Upgrade-Insecure-Requests':' 1',
  'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
  'Accept':' application/json, text/javascript, */*; q=0.01',
  'Accept-Language':' zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
  'Cookie':COOKIE, #改成自己的cookie,自己瀏覽器打開網(wǎng)站F12調(diào)試,自己找http請(qǐng)求的header
 }
 request = urllib.request.Request(url=urly,headers=headers)
 response = urllib.request.urlopen(request)
 contents = response.read()
 # 使用BS4獲得所有HTMLtag
 bsObj = BeautifulSoup(contents,"html.parser")
# 使用BS4的find函數(shù)得到想要的東西:標(biāo)題、發(fā)表時(shí)間和博客正文
 title = bsObj.find("h1").get_text()
 date = bsObj.find("span", attrs={"class":"pub-date"})
 dateT = bsObj.find("span", attrs={"class":"pub-date"}).get_text()
 text = bsObj.find("div", attrs={"id":"link-report"})
 # textT = bsObj.find("div", attrs={"class":"textCont"}).get_text()
# 測(cè)試輸出
 print(title)
 print(dateT)
 # 生成HTML文件。這里直接用file.open()和file.write()了,也可以用jinja2之類的框架生成。
 remove = string.whitespace+string.punctuation # 去掉日期的標(biāo)點(diǎn)符號(hào)
 table = str.maketrans(':',':',remove)
 fileTitle=str(title)+'-'+str(dateT).translate(table)+'.html'
 print(fileTitle) #測(cè)試輸出
 f = open(fileTitle,'w',encoding="utf-8") #注意用utf-8編碼寫入,不然會(huì)因?yàn)橐恍┡f博文采用的gbk編碼不兼容而出問題。
 # 寫入message
 message = """
 <html>
 <head></head>
 <body>
 <h1>%s</h1>
 <b>%s</b>
 <br></br>
 %s
 </body>
 </html>"""%(title,dateT,unicodedata.normalize('NFD',text.prettify()))
 f.write(message)
 f.close()
# 主循環(huán),給爺爬
boolean=0
while(boolean==0):
 a=1
 request(url)
 print('We finished page '+str(a)+' .')
 a+=1

Roadmap

豆瓣四月份時(shí)候還有bug,手機(jī)端可以看到全部日記,半年隱藏?zé)o效。最近修好了。

不過現(xiàn)在的隱藏依然沒有針對(duì)到具體的日記,或許可以想辦法通過其他手段爬下來(lái)。

以上就是python 開心網(wǎng)日記爬取的示例步驟的詳細(xì)內(nèi)容,更多關(guān)于python 開心網(wǎng)日記爬取的資料請(qǐng)關(guān)注本站其它相關(guān)文章!

國(guó)外服務(wù)器租用

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部