豆瓣爬虫《消失的她》

#code:utf-8
import requests
from bs4 import BeautifulSoup
import time

# 如果想多爬几页可以将16修改为更大的偶数
for i in range(2,20,2):
    url = 'https://movie.douban.com/subject/35660795/comments?start={}0&limit=20&status=P&sort=new_score'.format(i)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15'
    }

    # 请求
    r=requests.get(url, headers=headers)

    # 查看状态码
    print(r.status_code)

    # 获取标题
    html = BeautifulSoup(r.text,"html.parser")
    title = html.find("h1").text

    # 获取用户名、评论、评分
    divs = html.find_all("div", class_ = "comment")

    s = {"力荐":"*****","推荐":"****","还行":"***","较差":"**","很差":"*"}

    with open("{}.txt".format(title),"w+",encoding="utf-8") as f:
        f.write(str(["用户", "评分","data_time", "location","user_num","内容"]))

        for div in divs:
            print("---------------------------------")
            name = div.find("a", class_="").text
            print("用户名:",name)

            content = div.find("span", class_="short").text
            print("用户评论:",content)
            data_time = div.find("span", class_="comment-time")["title"]
            print("评论时间:",data_time)
            
            location = div.find("span", class_="comment-location").text
            print("评论地点:",location)
            
            
            user_num = div.find("span", class_="votes vote-count").text
            print("有用人数:",user_num)
            
            score = None
            for i in range(1,6):
                try:
                    score = s[div.find("span", class_="allstar{}0 rating".format(i))["title"]]
                except:
                    continue

            if score == None:
                score = "用户未评分"

            print("评分:",score)
            print("[+]...{}的评论已爬取".format(name))
            f.write("\n")
            f.write(str([name,score,data_time,location,user_num,content]))

        f.close()

以上是电影《消失的她》爬虫全部代码

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值