使用 Selenium 和 BeautifulSoup 爬取豆瓣电影影评

本文将介绍如何使用 Python 编写网络爬虫,利用 Selenium 和 BeautifulSoup 库,从豆瓣电影页面抓取影评信息。

1. 准备工作

首先,你需要安装以下库:

pip install requests beautifulsoup4 selenium

然后,你需要下载对应版本的 ChromeDriver 并将其添加到系统环境变量 PATH 中。

2. 代码实现

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 创建浏览器对象
browser = webdriver.Chrome()

# 访问目标页面
url = 'https://movie.douban.com/subject/25868125/'
browser.get(url)

# 设置浏览器窗口最大化
browser.maximize_window()

# 设置请求头部信息
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.36',
    'Cookie': '你的Cookie',
    'Referer': 'https://movie.douban.com/subject/25868125/'
}

# 等待页面加载完成
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.comment .comment-item')))

# 获取页面源代码
soup = BeautifulSoup(browser.page_source, 'html.parser')

# 解析页面内容获取影评数据
comments = soup.select('.comment-item')
for comment in comments:
    name = comment.select('.comment-info > a')[0].text.strip()
    time = comment.select('.comment-info > span')[0].text.strip()
    content = comment.select('.short')[0].text.strip()
    print(name, time, content)

# 循环爬取所有页的影评数据
while True:
    try:
        # 查找“下一页”按钮元素,若找到则点击,否则退出循环
        next_button = WebDriverWait(browser, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, '.comment .next a'))
        )
        next_button.click()
        # 等待页面加载完成
        WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.comment .comment-item')))
        soup = BeautifulSoup(browser.page_source, 'html.parser')
        comments = soup.select('.comment-item')
        for comment in comments:
            name = comment.select('.comment-info > a')[0].text.strip()
            time = comment.select('.comment-info > span')[0].text.strip()
            content = comment.select('.short')[0].text.strip()
            print(name, time, content)
    except:
        break

# 关闭浏览器
browser.close()

3. 代码说明

  1. 首先导入必要的库,包括 BeautifulSoup、selenium、WebDriverWait 和 expected_conditions 等。
  2. 创建浏览器对象并访问目标页面。
  3. 设置浏览器窗口最大化,并设置请求头部信息,包括 User-Agent、Cookie 和 Referer 等。
  4. 使用 WebDriverWait 等待页面加载完成,然后使用 BeautifulSoup 解析页面源代码。
  5. 循环遍历页面上的所有影评数据,提取评论者姓名、评论时间和评论内容。
  6. 循环爬取所有页的影评数据,使用 WebDriverWait 等待下一页按钮元素出现并点击。
  7. 在循环中使用 try-except 语句捕获异常,并在出现异常时关闭浏览器,避免资源浪费。

4. 代码优化

  1. 只使用一个浏览器对象,避免重复创建。
  2. 使用 WebDriverWait 等待页面加载完成,确保数据解析准确。
  3. 在循环中使用 try-except 语句捕获异常,并在出现异常时关闭浏览器,避免资源浪费。
  4. 使用浏览器对象的 get 方法访问页面,避免出现因为 Cookie 等信息不完整导致的访问失败。
  5. 使用 EC.element_to_be_clickable 方法判断下一页按钮是否可点击,避免一直等待无法退出循环。

5. 其他说明

  1. 代码中的 Cookie 需要替换成你的实际 Cookie,可以通过浏览器的开发者工具获取。
  2. 为了防止爬虫被网站识别和封锁,可以使用代理 IP,并设置合适的爬取频率。

希望本文能够帮助你学习使用 Python 编写网络爬虫,并从豆瓣电影页面抓取影评信息。如果你有任何疑问,欢迎在评论区留言。

豆瓣电影影评爬取:使用 Selenium 和 BeautifulSoup 抓取影评信息

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

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