爬取豆瓣电影的评论

 每天一爬


概要

本文章主要对电影的评论信息进行爬取,包括:用户,评分,评论时间,评论内容等。

一、使用模块

import requests
from pyquery import PyQuery as pq
from pymysql import Connection, MySQLError

二、反爬技术

1.请求头反爬

  • user-agent:豆瓣通过用户代理,判断是否是机器人,机器人返回418
  • cookie:短时间内,高强度访问豆瓣网,ip将被封锁,需要登陆才能访问

三、分析过程

1.观察评论的地址

 可以猜到1889243应该是这部电影的ID。

2.数据放在哪里

空白处右键,打开检查面板,选择网络选项卡,复制评论作为搜索关键词,发现评论嵌在html中

选择元素选项卡,随便定位一条评论,观察数据在html文档中的位置结构

3.其他数据在哪里

点击下一页,发现位置结构发生改变

 不难看出来,start=20&limit=20是查询条件,status=P是过滤条件,sort是排序方式。只有不断更新start的值就可以爬取所有数据。

4.怎么判断到尾

方法1:不断手动更新start的值跳转到最后一页,观察与前面页面的不同。发现最后一页的后页的超链接没有href这个属性,可将这个作为爬虫停止条件

 方法2:直接赋start超出范围值,跳转到豆瓣的错误页面,没有返回数据,也可以作为停止爬虫的条件。

四、完整代码

这里展示方法1的代码,方法2代码逻辑与之类似。

import requests
from pyquery import PyQuery as pq
from pymysql import Connection, MySQLError

# 设置请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0',
    'cookie': '你的cookie'
}

movie_id = 1889243
base_url = f'https://movie.douban.com/subject/{movie_id}/comments'
rating_table = {'很差': 1, '较差': 2, '还行': 3, '推荐': 4, '力荐': 5, None: 0}


def insert_comment(cursor, movie_id, comment_id, username, comment, rating, comment_time):
    try:
        cursor.execute(
            "INSERT INTO comments (movie_id, comment_id, username, comment, rating, comment_time) VALUES (%s, %s, %s, %s, %s, %s)",
            (movie_id, comment_id, username, comment, rating, comment_time)
        )
    except MySQLError as e:
        print(f"数据库插入错误: {e}")


def fetch_comments(url):
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return pq(response.text)


if __name__ == '__main__':
    with Connection(host='localhost', user='root', password='59420njdM', database='douban') as conn:
        cursor = conn.cursor()
        url = base_url + '?start=0&limit=20&status=P&sort=new_score'

        while True:
            doc = fetch_comments(url)
            for item in doc('#comments .comment-item').items():
                comment_id = item.attr('data-cid')
                comment = item.find('.short').text()
                username = item.find('.comment-info a').text()
                rating = rating_table[item.find('.rating').attr('title')]
                comment_time = item.find('.comment-time').text()

                print(f'评论ID: {comment_id}, 评论: {comment}, 用户: {username}, 评分: {rating}, 时间: {comment_time}')
                insert_comment(cursor, movie_id, comment_id, username, comment, rating, comment_time)

            conn.commit()  # 提交所有插入操作

            next_url = doc.find('.next').attr('href')
            if not next_url:
                break

            url = base_url + next_url
            print(f'下一页: {url}')

        cursor.close()
小结

这里只对一部电影的评论进行爬取,其余的电影可以结合前面的爬虫,实现爬取更多电影评论

爬取豆瓣电影评论,你可以使用Python编写一个爬虫程序。首先,你需要准备工作,包括导入相关库和设置爬取的网页地址。 你可以参考中提到的Python实现的豆瓣电影信息爬取功能来进行编写。根据这个例子,你可以使用`requests`库发送HTTP请求获取网页内容,使用`BeautifulSoup`库解析网页内容。你可以将评论的网页链接作为参数传递给爬虫程序。 为了简单起见,你可以先只爬取第一页的评论内容,然后根据需要逐步爬取更多的评论。你可以参考中提供的链接来修改爬取地址的start值来获取更多的评论内容。 请注意,豆瓣网站对爬虫有一定的限制,你需要设置合理的请求头信息来模拟浏览器访问。 在爬取评论内容时,你可以使用CSS选择器或XPath表达式来定位评论所在的HTML元素,并提取出评论内容。 最后,你可以将爬取到的评论保存到文件或数据库中,或者进行进一步的处理和分析。 总结起来,爬取豆瓣电影评论的步骤如下: 1. 准备工作,导入相关库,设置爬取网页地址; 2. 发送HTTP请求获取网页内容; 3. 使用BeautifulSoup解析网页内容,定位评论所在的HTML元素,并提取评论内容; 4. 根据需要修改爬取地址的start值获取更多的评论; 5.评论保存到文件或数据库中,或进行进一步处理和分析。 希望这个回答对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值