本次任务需要用到 requests、re 和 BeautifulSoup 库,代码如下:

import requests
import re
from bs4 import BeautifulSoup
import json

# 爬取第一页
url = 'https://movie.douban.com/subject/25868125/comments?start=0&limit=20&status=P&sort=new_score'

# 请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

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

# 解析网页
soup = BeautifulSoup(r.text, 'html.parser')

# 获取评论模块
comments = soup.find_all('div', {'class': 'comment'})

# 存储数据
data = []

# 遍历评论
for comment in comments:
    # 获取评论人名称
    name = comment.find('a', {'class': 'comment-info'}).text.strip()
    # 获取评论时间
    date = comment.find('span', {'class': 'comment-time'}).text.strip()
    # 获取评论内容
    content = comment.find('span', {'class': 'short'}).text.strip()
    
    # 存储数据
    data.append({'name': name, 'date': date, 'content': content})

# 爬取第2-3页
for i in range(1, 3):
    url = 'https://movie.douban.com/subject/25868125/comments?start=' + str(i*20) + '&limit=20&status=P&sort=new_score'
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')
    comments = soup.find_all('div', {'class': 'comment'})
    for comment in comments:
        name = comment.find('a', {'class': 'comment-info'}).text.strip()
        date = comment.find('span', {'class': 'comment-time'}).text.strip()
        content = comment.find('span', {'class': 'short'}).text.strip()
        data.append({'name': name, 'date': date, 'content': content})

# 存储为json格式
with open('comments.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)

代码注释:

  1. 导入所需要的库。

  2. 定义要爬取的起始地址。

  3. 请求头,伪装为浏览器。

  4. 发送请求,得到网页的 HTML 内容。

  5. 解析网页,使用 BeautifulSoup 库。

  6. 找到评论模块。

  7. 定义一个空列表,用于存储抓取到的数据。

  8. 遍历评论模块,获取评论人名称、评论时间以及评论。

  9. 将获取到的数据存储到列表中。

  10. 循环抓取第2-3页的数据,实现方法与第一页相似。

  11. 将数据存储为json格式的文件,使用 dump() 函数。其中,ensure_ascii=False 表示输出的文件不使用 ASCII 编码,indent=4 表示输出的文件按照缩进格式展示,易于阅读。

运行代码后,会在当前目录下生成一个名为 comments.json 的文件,里面存储了全部的评论人名称、评论时间以及评论


原文地址: http://www.cveoy.top/t/topic/g7iK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录