首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使用python在imdb中按下“加载更多”以获得更多评论

在IMDb中按下"加载更多"按钮以获取更多评论,可以使用Python编写一个脚本来模拟用户的操作并获取更多评论。以下是一个示例代码:

代码语言:python
复制
import requests
from bs4 import BeautifulSoup

def get_more_comments():
    url = "https://www.imdb.com/title/tt1375666/reviews?ref_=tt_urv"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }

    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content, "html.parser")

    load_more_button = soup.find("button", {"class": "ipl-load-more__button"})
    load_more_url = "https://www.imdb.com" + load_more_button["data-ajaxurl"]

    while load_more_button:
        response = requests.get(load_more_url, headers=headers)
        soup = BeautifulSoup(response.content, "html.parser")

        comments = soup.find_all("div", {"class": "text show-more__control"})
        for comment in comments:
            print(comment.text.strip())
            print("---")

        load_more_button = soup.find("button", {"class": "ipl-load-more__button"})
        if load_more_button:
            load_more_url = "https://www.imdb.com" + load_more_button["data-ajaxurl"]

get_more_comments()

这段代码使用了requests库来发送HTTP请求,并使用BeautifulSoup库来解析HTML内容。首先,我们发送一个GET请求到IMDb电影评论页面,然后使用BeautifulSoup解析页面内容。通过查找页面中的"加载更多"按钮,我们可以获取到加载更多评论的URL。接下来,我们循环发送GET请求到加载更多评论的URL,并解析返回的HTML内容。在每个页面中,我们找到评论的元素并打印出来。

请注意,这只是一个示例代码,实际上IMDb网站可能会有反爬虫机制,所以在实际使用中可能需要添加更多的处理逻辑,例如设置延时、使用代理等。

推荐的腾讯云相关产品和产品介绍链接地址:

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

1时5分

云拨测多方位主动式业务监控实战

1分23秒

如何平衡DC电源模块的体积和功率?

领券