【发布时间】:2021-11-14 13:56:34
【问题描述】:
我正在尝试从这里获取电影的所有评论:https://www.rottentomatoes.com/m/interstellar_2014/reviews。但正如您在网页上看到的那样,它们只显示大约 19 条评论。所以我无法获得所有评论我的代码下面只打印 19 条第一评论。
## First we import the module necessary to open URLs (basically websites)
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd
def scrapeUrl(URL):
""" scrape data from url - give url as a parameter """
page = urlopen(URL)
html_bytes = page.read()
html = html_bytes.decode("utf-8")
#print(HTML)
soup = BeautifulSoup(html, "html.parser")
return soup
def findReviews(soup):
""" find reviews using class="the_review" """
NoneType = type(None)
reviews = []
for element in soup.find_all("div"):
i = element.get("class")
if isinstance(i, NoneType) == False:
if 'the_review' in i:
reviews.append(element.text)
dfrev = pd.DataFrame(reviews, columns= ['reviews'])
return dfrev
url = "https://www.rottentomatoes.com/m/interstellar_2014/reviews"
sc = scrapeUrl(URL)
t = findReviews(sc)
print(t)
【问题讨论】:
标签: python web-scraping beautifulsoup