【发布时间】:2019-10-09 04:59:15
【问题描述】:
我正在开展一个学校项目,并希望获得所有用户对 IMDB 超级英雄电影的评论。
首先,我尝试仅获取一部电影的所有用户评论。
用户评论页面,由 25 条用户评论和一个“加载更多”按钮组成。虽然我已经设法编写代码来打开加载更多按钮。我陷入了第二部分:将所有用户评论放在一个列表中。
我已经尝试使用 BeautifulSoup 来查找页面上的所有“内容”部分。但是,我的清单仍然是空的。
from bs4 import BeautifulSoup
testurl = "https://www.imdb.com/title/tt0357277/reviews?ref_=tt_urv"
patience_time1 = 60
XPATH_loadmore = "//*[@id='load-more-trigger']"
XPATH_grade = "//*[@class='review-container']/div[1]"
list_grades = []
driver = webdriver.Firefox()
driver.get(testurl)
# This is the part in which I open all 'load more' buttons.
while True:
try:
loadmore = driver.find_element_by_id("load-more-trigger")
time.sleep(2)
loadmore.click()
time.sleep(5)
except Exception as e:
print(e)
break
print("Complete")
time.sleep(10)
# When the whole page is loaded, I want to get all 'content' parts.
soup = BeautifulSoup(driver.page_source)
content = soup.findAll("content")
list_content = [c.text_content() for c in content]
driver.quit()
我希望获得网站上所有评论容器内容的列表。但是,我的列表仍然是空的。
【问题讨论】:
-
你有没有看一下当你点击加载更多时会发生什么请求?相反,复制请求可能更容易。
-
我在本地运行您的代码时看到
name 'webdriver' is not defined。你能提供一个requirements.txt吗? -
@Jeff Xiao 我导入了以下模块: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import NoSuchElementException import time
-
@Marieke 我已经添加了答案。另一个注意事项是您可能需要调整睡眠时间,目前它在我的机器上过长。
标签: python selenium web-scraping beautifulsoup findall