欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python抓取豆瓣影評數(shù)據(jù)的方法

 更新時間:2018年10月17日 10:34:22   作者:no-96  
今天小編就為大家分享一篇關(guān)于使用Python抓取豆瓣影評數(shù)據(jù)的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

抓取豆瓣影評評分

正常的抓取

分析請求的url

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

  • 設(shè)計查詢列表,發(fā)現(xiàn)頁面中有url中的查詢部分,且指向下一個頁面

于是采用下面的代碼進(jìn)行判斷是否還有下一個頁面

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

把數(shù)據(jù)寫入txt

import requests
from bs4 import BeautifulSoup
first_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('支持人數(shù):'+votes+'\n')
      file.write('評論內(nèi)容:'+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端的簡單,更加容易解析,這次模擬移動端,發(fā)現(xiàn)可以直接訪問api獲取json格式的數(shù)據(jù),nice!

至于怎么模擬移動端只需要將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 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'
# 移動端頭部信息
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('評論內(nèi)容:'+item['comment']+'\n')
      else:
        file.write('評論內(nèi)容: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)


總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • python表格存取的方法

    python表格存取的方法

    這篇文章主要為大家詳細(xì)介紹了python表格存取的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Python基礎(chǔ)學(xué)習(xí)之奇異的GUI對話框

    Python基礎(chǔ)學(xué)習(xí)之奇異的GUI對話框

    今天跨進(jìn)了GUI編程的園地,才發(fā)現(xiàn)python語言是這么的好玩,文中對GUI對話框作了非常詳細(xì)的介紹,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • 深入了解Python的類與模塊化

    深入了解Python的類與模塊化

    這篇文章主要介紹了Python中的面向?qū)ο缶幊谭妒揭约澳K化思想,并給出相應(yīng)的實戰(zhàn)示例及解釋,對我們的學(xué)習(xí)和工作都有一定的價值,感興趣的小伙伴可以了解一下
    2021-12-12
  • python中的yield使用方法

    python中的yield使用方法

    這篇文章主要介紹了python中的yield使用方法,需要的朋友可以參考下
    2014-02-02
  • 解析Pytorch中的torch.gather()函數(shù)

    解析Pytorch中的torch.gather()函數(shù)

    本文給大家介紹了Pytorch中的torch.gather()函數(shù),通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-11-11
  • python快排算法詳解

    python快排算法詳解

    在本篇內(nèi)容里小編給大家整理了關(guān)于python快排算法的相關(guān)知識點內(nèi)基礎(chǔ)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-03-03
  • python中Lambda表達(dá)式詳解

    python中Lambda表達(dá)式詳解

    在本篇文章里小編給大家整理的是關(guān)于python中Lambda表達(dá)式的相關(guān)知識點內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-11-11
  • django執(zhí)行原生SQL查詢的實現(xiàn)

    django執(zhí)行原生SQL查詢的實現(xiàn)

    本文主要介紹了django執(zhí)行原生SQL查詢的實現(xiàn),主要有兩種方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 如何通過Python實現(xiàn)定時打卡小程序

    如何通過Python實現(xiàn)定時打卡小程序

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)定時打卡小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • python Selenium 庫的使用技巧

    python Selenium 庫的使用技巧

    這篇文章主要介紹了python Selenium 庫的使用技巧,幫助大家更好的理解和學(xué)習(xí)python Selenium 庫,感興趣的朋友可以了解下
    2020-10-10

最新評論