爬豆瓣封神第一部:朝歌风云 短评

import pandas as pd
import requests
import re
total=[]
class Spider_db:
    def __init__(self):
        self.comment_url = "https://movie.douban.com/subject/10604086/comments?start={}&limit=20&status=P&sort=new_score"
        self.comment_headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
            "cookie":'####'}  # 需要您自行登录豆瓣后,F12-->抓包--->得到您的cookie信息,然后替换即可
    def get_comment_url(self):
        url_list = []
        for i in range(30):
            url_list.append(self.comment_url.format(i * 20))
        return url_list
    def  comment_db(self, url):
        resp = requests.get(url=url, headers=self.comment_headers)
        comments = re.findall('<span class="short">(.*)</span>', resp.content.decode())
        id = re.findall('<div.*?="avatar".*?<a.*?="(.*?)".*?</a>', resp.text, re.S)
        you=re.findall('<span class="votes vote-count">(.*)</span>', resp.content.decode())
        data=re.findall('<span class="comment-time " title="(.+?)">', resp.content.decode())
        pf = re.findall('<span class="allstar[0-9]+ rating" title="(.+?)">', resp.content.decode())
        add=re.findall('<span class="comment-location">(.*?)</span>', resp.text, re.S)
        print(add)
        for comments,you,data,pf,id,add in zip(comments,you,data,pf,id,add):
            info = {"ID":id,
                    '评论': comments,
                    '有用指数':you,
                    "时间":data,
                    "推荐指数":pf,
                    "地址":add
                    }
            total.append(info)
    def run(self):
        # 1.页面规律总结,得到要爬取数据的页面
        comment_list=self.get_comment_url()
        for url in comment_list:
            self.comment_db(url)
            comment_list.index(url) + 1
        print("shue")
if __name__ == '__main__':
    spider = Spider_db()
    spider.run()
df = pd.DataFrame(total)
df.to_csv('封神第一部:朝歌风云 短评.csv',encoding='utf_8_sig')

 自己摸索中,如有错误或者更换的欢迎指导,谢谢

执行后截图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值