以下是使用Python语言使用网络爬虫技术,抓取《穿靴子的猫2》在豆瓣电影上的所有页的影评数据的代码:

import requests
from bs4 import BeautifulSoup

url = 'https://movie.douban.com/subject/25868125/comments'
params = {'start': 0, 'limit': 20, 'status': 'P', 'sort': 'new_score'}

while True:
    response = requests.get(url, params=params)
    soup = BeautifulSoup(response.text, 'html.parser')
    comments = soup.find_all('div', class_='comment-item')

    for comment in comments:
        username = comment.find('span', class_='comment-info').find('a').text.strip()
        comment_time = comment.find('span', class_='comment-time').text.strip()
        comment_text = comment.find('span', class_='short').text.strip()
        print(username, comment_time, comment_text)

    next_link = soup.find('link', rel='next')
    if not next_link:
        break
    url = next_link['href']
    params = None

以上代码中,首先定义了起始的URL地址和参数。然后使用循环来遍历每一页的评论数据。在循环中,先发送HTTP请求,获取响应内容。然后使用BeautifulSoup库解析HTML内容,找到所有影评数据的标签。对于每条影评数据,使用find方法找到对应的用户名、评论时间和评论内容,并打印输出。最后找到下一页的链接地址,如果存在下一页,则更新URL参数,再次发送HTTP请求,进入下一页。如果没有下一页,则退出循环


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

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