設計思路:
重點及難點:
解決方法:
對於沒有推薦理由的影片,程式會報錯,使用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=
"該片沒有推薦理由"
豆瓣top250簡易爬蟲
1.爬取目標是豆瓣top250 只要電影的名字 python基礎 檔案操作,字串拼接,for和while迴圈 requests庫的基礎使用 re庫的使用 import requests import re url start num 0while start num 225 拼接url parame...
Python爬蟲實戰 豆瓣電影top250
很多天沒有發部落格了,這幾天在弄乙個文字相似度的專案,問題乙個接乙個,忙活了好幾天。今天分享一下之前的寫的爬蟲,用來爬豆瓣電影的top250。首先,f12看看電影的資訊在 每個電影的資訊都在這個class item 的塊中,好的,這下好辦了,找到這個塊,就可以乙個個把裡面的東西抓出來了。好了,原始碼...
Python 爬蟲 抓取豆瓣讀書TOP250
coding utf 8 author yukun import requests from bs4 import beautifulsoup 發出請求獲得html原始碼的函式 def get html url 偽裝成瀏覽器訪問 resp requests.get url,headers heade...