这里提供一个简单的爬取豆瓣电影评论的代码,仅供参考:

import requests
from bs4 import BeautifulSoup

url = 'https://movie.douban.com/subject/34841067/comments?start=0&limit=20&sort=new_score&status=P'
# 替换为你想要爬取评论的电影链接

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'}

def get_comments(url):
    res = requests.get(url, headers=headers)
    soup = BeautifulSoup(res.text, 'html.parser')
    comments = soup.find_all('span', {'class': 'short'})
    for comment in comments:
        print(comment.text)

for i in range(0, 100, 20):
    url = 'https://movie.douban.com/subject/34841067/comments?start={}&limit=20&sort=new_score&status=P'.format(i)
    get_comments(url)

该代码使用 requests 库发送网络请求,使用 BeautifulSoup 库解析 HTML 页面,提取评论内容并输出。其中,通过循环爬取多个页面,实现了爬取更多的评论。不过需要注意的是,爬取他人网站数据需要遵守相关法律法规,以及伦理道德。

帮我用python写一段爬取评论的代码

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

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