欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Python抓取豆瓣影评数据的方法

程序员文章站 2023-08-26 20:16:00
抓取豆瓣影评评分 正常的抓取 分析请求的url https://movie.douban.com/subject/26322642/comments...

抓取豆瓣影评评分

正常的抓取

分析请求的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 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('支持人数:'+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 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('评论内容:'+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)


总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接