由于 ChromeDriver 版本不匹配导致无法创建会话,需要更新 ChromeDriver 或使用与当前 Chrome 版本匹配的 ChromeDriver。

以下是抓取《穿靴子的猫2》在豆瓣电影上的所有页的影评数据的示例代码:

from selenium import webdriver
import time

# 打开浏览器
browser = webdriver.Chrome()
browser.maximize_window()

# 进入电影页面
url = 'https://movie.douban.com/subject/25868125/'
browser.get(url)

# 点击全部影评
all_review_button = browser.find_element_by_xpath('//a[@class="more"]')
all_review_button.click()

# 获取第一页的评论人名称、评论时间、评论
review_list = browser.find_elements_by_xpath('//div[@class="comment-item"]')
for review in review_list:
    name = review.find_element_by_xpath('.//span[@class="comment-info"]/a').text
    time = review.find_element_by_xpath('.//span[@class="comment-time"]').text
    content = review.find_element_by_xpath('.//p/span').text
    print(name, time, content)

# 循环获取所有影评数据
while True:
    next_page_button = browser.find_element_by_xpath('//a[@class="next"]')
    if 'disabled' in next_page_button.get_attribute('class'):
        break
    next_page_button.click()
    time.sleep(1)  # 等待页面加载完成
    review_list = browser.find_elements_by_xpath('//div[@class="comment-item"]')
    for review in review_list:
        name = review.find_element_by_xpath('.//span[@class="comment-info"]/a').text
        time = review.find_element_by_xpath('.//span[@class="comment-time"]').text
        content = review.find_element_by_xpath('.//p/span').text
        print(name, time, content)

# 关闭浏览器
browser.quit()

该代码会打开 Chrome 浏览器,并自动点击进入全部影评页面,然后获取第一页的评论人名称、评论时间和评论,并循环获取所有影评数据。需要注意的是,为了避免页面加载过慢导致获取不到数据,需要在翻页后等待一段时间,可以根据实际情况调整等待时间。

Selenium 抓取《穿靴子的猫2》豆瓣影评:ChromeDriver 版本不匹配解决方法

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

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