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 import time

设置Chrome浏览器路径

browser = webdriver.Chrome('D:/chromedriver.exe')

打开网页

url = 'https://movie.douban.com/subject/25868125/' browser.get(url)

点击‘全部影评’

button = browser.find_element_by_xpath('//*[@id="comments-section"]/div[1]/h2/span/a') button.click()

等待网页加载完成

wait = WebDriverWait(browser, 10) wait.until(EC.presence_of_element_located((By.ID, 'comments-section')))

读取网页源代码

html = browser.page_source soup = BeautifulSoup(html, 'html.parser')

获取第一页评论

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

获取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.until(EC.presence_of_element_located((By.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}) # 等待3秒钟,避免频繁访问网页被封IP time.sleep(3)

存储为json文件

with open('comments.json', 'w', encoding='utf-8') as f: json.dump(comments, f, ensure_ascii=False)

关闭浏览器

browser.quit()

豆瓣电影《我不是药神》评论数据爬取 - Python 代码实现

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

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