国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

使用Python抓取豆瓣影評數據的方法

2020-01-04 14:20:08
字體:
來源:轉載
供稿:網友

抓取豆瓣影評評分

正常的抓取

分析請求的url

https://movie.douban.com/subject/26322642/comments?start=20&limit=20&sort=new_score&status=P&percent_type=

里面有用的也就是startlimit參數,我嘗試過修改limit參數,但是沒有效果,可以認為是默認的 
start參數是用來設置從第幾條數據開始查詢的

  • 設計查詢列表,發現頁面中有url中的查詢部分,且指向下一個頁面

Python,抓取,豆瓣影評

于是采用下面的代碼進行判斷是否還有下一個頁面

if next_url:    visit_URL('https://movie.douban.com/subject/24753477/comments'+next_url)
  • 用requests發送請求,beautifulsoup進行網頁解析

Python,抓取,豆瓣影評

把數據寫入txt

import requestsfrom bs4 import BeautifulSoupfirst_url = 'https://movie.douban.com/subject/26322642/comments?status=P'# 請求頭部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') # 找到所有的評論模塊  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('評論人:'+username+'/n')      file.write('評論時間:'+comment_time+'/n')      file.write('支持人數:'+votes+'/n')      file.write('評論內容:'+comment+'/n')  # 檢查是否有下一頁  next_url = soup.find('a',class_='next')  if next_url:    temp = next_url['href'].strip().split('&') # 獲取下一個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)

模仿移動端

很多時候模仿移動端獲得的頁面會比PC端的簡單,更加容易解析,這次模擬移動端,發現可以直接訪問api獲取json格式的數據,nice!

Python,抓取,豆瓣影評

至于怎么模擬移動端只需要將user-agent修改為移動端的頭

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 randomimport requestsimport jsonimport timefirst_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'# 移動端頭部信息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)  # 請求頭部  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('評論用戶:'+item['user']['name']+'/n')      else:        file.write('評論用戶:none/n')      if item['create_time']:        file.write('評論時間:'+item['create_time']+'/n')      else:        file.write('評論時間:none/n')      if item['comment']:        file.write('評論內容:'+item['comment']+'/n')      else:        file.write('評論內容:none/n')      if item['rating']:        if item['rating']['value']:          file.write('對電影的評分:'+str(item['rating']['value'])+'/n/n')      else:        file.write('對電影的評分:none/n')if __name__ == '__main__':  for i in range(0,66891,20):    # time.sleep(2)    visit_URL(i)

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武清区| 申扎县| 花垣县| 吉木萨尔县| 巩留县| 吴川市| 潜山县| 买车| 上高县| 平罗县| 潍坊市| 汉中市| 云和县| 宁波市| 衡阳市| 井冈山市| 梅河口市| 沙河市| 永仁县| 商丘市| 溧水县| 青铜峡市| 翁源县| 育儿| 开鲁县| 惠来县| 若尔盖县| 河曲县| 英山县| 修文县| 邓州市| 沅江市| 威远县| 阿图什市| 浮山县| 沂南县| 榆林市| 永济市| 肥东县| 偃师市| 四会市|