豆瓣資料爬蟲

2021-10-13 21:08:36 字數 1847 閱讀 8880

豆瓣資料爬蟲

設計思路:

通過url,去網頁上爬取豆瓣的影片資訊。

獲取url

獲取網頁原始檔文字內容

切片獲取所需資源

建立資料夾

將資訊寫入文字檔案

重點及難點:

網頁反爬蟲,人機檢驗

有的影片沒有推薦理由

新建資料夾

有的影片沒有推薦理由

對於沒有推薦理由的影片,程式會報錯,使用try…except…語句進行異常捕捉,出現異常時返回沒有推薦理由的文字資訊。

新建資料夾

使用python內建模板os的方法makedirs(「新建資料夾路徑」)

實際**:

from urllib import request

import os

import time

from random import randint

user_agent=

#user agent為字典形式

for a in

range(0

,10):

req=request.request(url=

"".format

(a*25

),headers=user_agent)

#頭封裝

html=request.urlopen(req)

text_html=html.read(

).decode(

)for i in

range(1

,26):

rank=text_html.split("")

[i].split(")[

1].split(

">")[

1].split(

"<")[

0]name=text_html.split("")

[i].split(

"title")[

1].split(

">")[

1].split(

"<")[

0]try:

info=text_html.split("")

[i].split(

"inq")[

1].split(

">")[

1].split(

"<")[

0]except indexerror:

info=

"該片沒有推薦理由"

python爬蟲 豆瓣

在爬蟲時候,通過requests.get 得到不正確的狀態碼 4 表示出錯 異常狀態碼檢視 fr aladdin 4 19 用requests庫的get 函式抓取乙個你平時經常使用的 主頁,並檢視該 的爬蟲協議 如果有的話 預設答案是正確,你成功了嗎?fp.write r.content b.有些 ...

豆瓣top250資料爬蟲

設計思路 重點及難點 解決方法 對於沒有推薦理由的影片,程式會報錯,使用try except 語句進行異常捕捉,出現異常時返回沒有推薦理由的文字資訊。使用python內建模板os的方法makedirs 新建資料夾路徑 實際 from urllib import request import os i...

python爬蟲 豆瓣電影

最近學習python 順便寫下爬蟲練手 爬的是豆瓣電影排行榜 python版本2.7.6 安裝 beautiful soup sudo apt get install python bs4 安裝 requests sudo apt get install python requests下面是py a...