如何使用BeautifulGroup获取所有IMDB用户对movi的评论

2024-04-23 15:09:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个学校项目,希望得到所有用户对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()

我希望得到一个所有内容的审查容器在网站上。然而,我的名单仍然是空的。在


Tags: 用户内容电影timemoredriver评论load
1条回答
网友
1楼 · 发布于 2024-04-23 15:09:28

你用的是beauthulsoup4,对吗?在

方法名从3改为4。(document

另外,find_all接受标记名,以及css类的可选class_参数(请参见SO answer

所以您的代码应该使用新名称:

    # content = soup.findAll("content")
    content = soup.find_all('div', class_=['text','show-more__control'])

在你的列表理解中也使用get_text()

^{pr2}$

最后,在获取soup时提供一个解析器:(document

^{3}$

否则,您将遇到以下用户警告:

SO56261323.py:36: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

相关问题 更多 >