使用Python抓取豆瓣影評(píng)數(shù)據(jù)的方法
抓取豆瓣影評(píng)評(píng)分
正常的抓取
分析請(qǐng)求的url
里面有用的也就是start和limit參數(shù),我嘗試過(guò)修改limit參數(shù),但是沒(méi)有效果,可以認(rèn)為是默認(rèn)的
start參數(shù)是用來(lái)設(shè)置從第幾條數(shù)據(jù)開(kāi)始查詢的
- 設(shè)計(jì)查詢列表,發(fā)現(xiàn)頁(yè)面中有url中的查詢部分,且指向下一個(gè)頁(yè)面

于是采用下面的代碼進(jìn)行判斷是否還有下一個(gè)頁(yè)面
if next_url:
visit_URL('https://movie.douban.com/subject/24753477/comments'+next_url)
- 用requests發(fā)送請(qǐng)求,beautifulsoup進(jìn)行網(wǎng)頁(yè)解析

把數(shù)據(jù)寫(xiě)入txt
import requests
from bs4 import BeautifulSoup
first_url = 'https://movie.douban.com/subject/26322642/comments?status=P'
# 請(qǐng)求頭部
headers = {
'Host':'movie.douban.com',
'Referer':'https://movie.douban.com/subject/24753477/?tag=%E7%83%AD%E9%97%A8&from=gaia_video',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
}
def visit_URL(url):
res = requests.get(url=url,headers=headers)
soup = BeautifulSoup(res.content,'html5lib')
div_comment = soup.find_all('div',class_='comment-item') # 找到所有的評(píng)論模塊
for com in div_comment:
username = com.find('div',class_='avatar').a['title']
comment_time = com.find('span',class_='comment-time')['title']
votes = com.find('span',class_='votes').get_text()
comment = com.p.get_text()
with open('1.txt','a',encoding='utf8') as file:
file.write('評(píng)論人:'+username+'\n')
file.write('評(píng)論時(shí)間:'+comment_time+'\n')
file.write('支持人數(shù):'+votes+'\n')
file.write('評(píng)論內(nèi)容:'+comment+'\n')
# 檢查是否有下一頁(yè)
next_url = soup.find('a',class_='next')
if next_url:
temp = next_url['href'].strip().split('&') # 獲取下一個(gè)url
next_url = ''.join(temp)
print(next_url)
# print(next_url)
if next_url:
visit_URL('https://movie.douban.com/subject/24753477/comments'+next_url)
if __name__ == '__main__':
visit_URL(first_url)
模仿移動(dòng)端
很多時(shí)候模仿移動(dòng)端獲得的頁(yè)面會(huì)比PC端的簡(jiǎn)單,更加容易解析,這次模擬移動(dòng)端,發(fā)現(xiàn)可以直接訪問(wèn)api獲取json格式的數(shù)據(jù),nice!

至于怎么模擬移動(dòng)端只需要將user-agent修改為移動(dòng)端的頭
useragents = [ "Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/47.0.2526.70 Mobile/13C71 Safari/601.1.46", "Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)"
怎么獲取這些頭部?用火狐的插件user-agent switcher
之后的操作就是解析json
import random
import requests
import json
import time
first_url = 'https://m.douban.com/rexxar/api/v2/tv/26322642/interests?count=20&order_by=hot&start=0&ck=dNhr&for_mobile=1'
url = 'https://m.douban.com/rexxar/api/v2/tv/26322642/interests'
# 移動(dòng)端頭部信息
useragents = [
"Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/47.0.2526.70 Mobile/13C71 Safari/601.1.46",
"Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)"
]
def visit_URL(i):
print(">>>>>",i)
# 請(qǐng)求頭部
headers = {
'Host':'m.douban.com',
'Upgrade-Insecure-Requests':'1',
'User-Agent':random.choice(useragents)
}
params = {
'count':'50',
'order_by':'hot',
'start':str(i),
'for_mobile':'1',
'ck':'dNhr'
}
res = requests.get(url=url,headers=headers,params=params)
res_json = res.json()
interests = res_json['interests']
print(len(interests))
for item in interests:
with open('huge.txt','a',encoding='utf-8') as file:
if item['user']:
if item['user']['name']:
file.write('評(píng)論用戶:'+item['user']['name']+'\n')
else:
file.write('評(píng)論用戶:none\n')
if item['create_time']:
file.write('評(píng)論時(shí)間:'+item['create_time']+'\n')
else:
file.write('評(píng)論時(shí)間:none\n')
if item['comment']:
file.write('評(píng)論內(nèi)容:'+item['comment']+'\n')
else:
file.write('評(píng)論內(nèi)容:none\n')
if item['rating']:
if item['rating']['value']:
file.write('對(duì)電影的評(píng)分:'+str(item['rating']['value'])+'\n\n')
else:
file.write('對(duì)電影的評(píng)分:none\n')
if __name__ == '__main__':
for i in range(0,66891,20):
# time.sleep(2)
visit_URL(i)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
python2.7+selenium2實(shí)現(xiàn)淘寶滑塊自動(dòng)認(rèn)證功能
這篇文章主要為大家詳細(xì)介紹了python2.7+selenium2實(shí)現(xiàn)淘寶滑塊自動(dòng)認(rèn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
python 解決flask uwsgi 獲取不到全局變量的問(wèn)題
今天小編就為大家分享一篇python 解決flask uwsgi 獲取不到全局變量的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
pytorch預(yù)測(cè)之解決多次預(yù)測(cè)結(jié)果不一致問(wèn)題
這篇文章主要介紹了pytorch多次預(yù)測(cè)結(jié)果不一致的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Python如何將JavaScript轉(zhuǎn)換為json
文章介紹了如何使用Python的re模塊將JavaScript代碼轉(zhuǎn)換為JSON格式,首先,使用正則表達(dá)式匹配并替換JavaScript代碼中的數(shù)字,確保它們被雙引號(hào)括起來(lái),然后,使用另一個(gè)正則表達(dá)式匹配并替換JavaScript代碼中的鍵值對(duì),確保鍵和值都被雙引號(hào)括起來(lái)2025-02-02
Python實(shí)現(xiàn)圖書(shū)借閱管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)圖書(shū)借閱管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
pd.read_csv讀取文件路徑出現(xiàn)的問(wèn)題解決
本文主要介紹了pd.read_csv讀取文件路徑出現(xiàn)的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
使用Python隨機(jī)生成數(shù)據(jù)的方法
這篇文章主要介紹了使用Python隨機(jī)生成數(shù)據(jù)的方法,在日常開(kāi)發(fā)中竟然會(huì)遇到需要測(cè)試大量數(shù)據(jù)的地方,那么隨機(jī)生成數(shù)據(jù)就可以有效的加快我們的效率,通過(guò)Python_Faker生成測(cè)試數(shù)據(jù)需要安裝Faker包,需要的朋友可以參考下2023-10-10
利用Python批量識(shí)別電子賬單數(shù)據(jù)的方法
這篇文章主要介紹了利用Python批量識(shí)別電子賬單數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

