豆瓣电影评论爬取 - Python Selenium & BeautifulSoup
豆瓣电影评论爬取 - Python Selenium & BeautifulSoup
本文将使用 Python 结合 Selenium 和 BeautifulSoup 库爬取豆瓣电影的评论,并保存为 JSON 格式文件。
1. 导入必要的库
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
from bs4 import BeautifulSoup
import json
2. 设置浏览器路径和目标网页
# 设置Chrome浏览器路径
browser = webdriver.Chrome('D:/chromedriver.exe')
# 打开网页
url = 'https://movie.douban.com/subject/25868125/'
browser.get(url)
3. 点击“全部影评”按钮
# 点击“全部影评”
button = browser.find_element_by_xpath('//*[@id='comments-section']/div[1]/h2/span/a')
button.click()
4. 等待网页加载完成
# 等待网页加载完成
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, 'comments-section')))
5. 读取网页源代码并解析
# 读取网页源代码
html = browser.page_source
soup = BeautifulSoup(html, 'html.parser')
6. 获取第一页评论
# 获取第一页评论
comments = []
for item in soup.find_all('div', class_='comment-item'):
name = item.find('a', class_='').get_text().strip() # 评论人名称
time = item.find('span', class_='comment-time').get_text().strip() # 评论时间
content = item.find('span', class_='short').get_text().strip() # 评论内容
comments.append({'name': name, 'time': time, 'content': content})
7. 获取2-3页评论
# 获取2-3页评论
for i in range(1, 3):
url = 'https://movie.douban.com/subject/25868125/comments?start={}&limit=20&status=P&sort=new_score'.format(i*20)
browser.get(url)
wait = WebDriverWait(browser, 10) # 设置等待时间为10秒
wait.until(EC.presence_of_element_located((By.ID, 'comments-section'))) # 等待ID为comments-section的元素加载完成
html = browser.page_source
soup = BeautifulSoup(html, 'html.parser')
for item in soup.find_all('div', class_='comment-item'):
name = item.find('a', class_='').get_text().strip() # 评论人名称
time = item.find('span', class_='comment-time').get_text().strip() # 评论时间
content = item.find('span', class_='short').get_text().strip() # 评论内容
comments.append({'name': name, 'time': time, 'content': content})
8. 存储为JSON文件
# 存储为json文件
with open('comments.json', 'w', encoding='utf-8') as f:
json.dump(comments, f, ensure_ascii=False)
9. 关闭浏览器
# 关闭浏览器
browser.quit()
如何增设等待时间
可以将等待时间添加在以下两个地方:
- 在点击“全部影评”按钮后,等待网页加载完成。
button.click()
wait = WebDriverWait(browser, 10) # 设置等待时间为10秒
wait.until(EC.presence_of_element_located((By.ID, 'comments-section'))) # 等待ID为comments-section的元素加载完成
- 在获取2-3页评论时,等待网页加载完成。
for i in range(1, 3):
url = 'https://movie.douban.com/subject/25868125/comments?start={}&limit=20&status=P&sort=new_score'.format(i*20)
browser.get(url)
wait = WebDriverWait(browser, 10) # 设置等待时间为10秒
wait.until(EC.presence_of_element_located((By.ID, 'comments-section'))) # 等待ID为comments-section的元素加载完成
html = browser.page_source
soup = BeautifulSoup(html, 'html.parser')
for item in soup.find_all('div', class_='comment-item'):
name = item.find('a', class_='').get_text().strip() # 评论人名称
time = item.find('span', class_='comment-time').get_text().strip() # 评论时间
content = item.find('span', class_='short').get_text().strip() # 评论内容
comments.append({'name': name, 'time': time, 'content': content})
通过以上步骤,就可以成功爬取豆瓣电影的评论并保存为JSON格式文件了。
注意: 爬取网站数据时,请遵守网站的robots协议,并注意不要过度爬取,以免对网站造成负担。
原文地址: https://www.cveoy.top/t/topic/oBgw 著作权归作者所有。请勿转载和采集!