我正在 T-Mobile 网站上搜索有关三星 Galaxy S9 的评论。我能够为 HTML 代码创建一个 Beautiful Soup 对象,但我无法获取存在于 span 类中的评论文本,还需要遍历评论页面以收集所有评论。
我尝试了 2 个代码,但一个返回错误,另一个返回空列表。我也无法在汤对象中找到我需要的特定跨度类。
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
tmo_ratings_s9 = []
req = Request('https://www.t-mobile.com/cell-phone/samsung-galaxy-s9', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
tmo_soup_s9 = BeautifulSoup(webpage, 'html.parser')
tmo_soup_s9.prettify()
for review in tmo_soup_s9.find_all(class_="BVRRReviewText"):
text = review.span.get_text(strip=True)
tmo_soup_s9.append(text)
print(tmo_ratings_s9)
############################################################################
from urllib.request import urlopen
html = urlopen("https://www.t-mobile.com/cell-phone/samsung-galaxy-s9")
soup=BeautifulSoup(html)
ratings = soup.find_all('div', class_='BVRRReviewTextParagraph BVRRReviewTextFirstParagraph BVRRReviewTextLastParagraph')
textofrep = ratings.get_text().strip()
tmo_ratings_s9.append(textofrep)
我希望从网页上的所有 8 个页面中获取评论文本并将它们存储在一个 HTML 文件中。
慕桂英3389331
元芳怎么了
HUX布斯
相关分类