我正在做一个学校项目,希望得到所有用户对IMDB超级英雄电影的评论。在
首先,我试着只得到一部电影的所有用户评论。在
用户评论页面,包括25个用户评论和一个“加载更多”按钮。而我已经设法写了一个代码来打开加载更多按钮。我陷入了第二部分:在一个列表中获取所有用户评论。在
我已经尝试过用beauthoulsoup来查找页面上所有的“内容”部分。然而,我的名单仍然是空的。在
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()
我希望得到一个所有内容的审查容器在网站上。然而,我的名单仍然是空的。在
你用的是beauthulsoup4,对吗?在
方法名从3改为4。(document)
另外,
find_all
接受标记名,以及css类的可选class_
参数(请参见SO answer)所以您的代码应该使用新名称:
在你的列表理解中也使用
^{pr2}$get_text()
:最后,在获取soup时提供一个解析器:(document)
^{3}$否则,您将遇到以下用户警告:
相关问题 更多 >
编程相关推荐