【发布时间】:2020-05-20 03:23:26
【问题描述】:
您好,我正在尝试从网站获取一些信息。如果我的格式有任何错误,请原谅我这是我第一次在 SO 上发帖。
soup.find('div', {"class":"stars"})
从这里我收到了
<div class="stars" title="4.0 star rating">
<i class="star star--large star-0"></i><i class="star star--large star-
1"></i><i class="star star--large star-2"></i><i class="star star--large
star-3"></i><i class="star star--large star-4 star--large--muted"></i>
</div>
我需要那个"4.0 star rating"
当我使用时:
soup.find('div', {"class":"stars"})["title"]
它有效,但不适用于 find_all。但我试图找到所有案例并将它们放入列表中。
这是我下面的完整代码。
def get_info():
from IPython.display import HTML
import requests
from bs4 import BeautifulSoup
n = 1
for page in range(53):
url = f"https://www.sitejabber.com/reviews/apple.com?page=
{n}&sort=Reviews.processed&direction=DESC#reviews"
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
all_reviews = soup.find_all('div', {'class':"truncate_review"})
all_dates = soup.find_all('div', {'class':'review__date'},'title')
all_titles = soup.find_all('span', {'class':'review__title__text'})
reviews_class = soup.find('div', {"class":"review__stars"})
for review in all_reviews:
all_reviews_list.append(review.text.replace("\n","").replace("\t",""))
for date in all_dates:
all_dates_list.append(date.text.replace("\n","").replace("\t",""))
for title in all_titles:
all_titles_list.append(title.text.replace("\n","").replace("\t",""))
for stars in reviews_class.find_all('div', {'class':'stars'}):
all_star_ratings.append(stars['title'])
n += 1
对不起,我的缩进有点乱,但这是我的完整代码。
【问题讨论】:
-
当你在 rang() 中做页面时,你需要另一个变量 n 吗?
-
@JoshuaVarghese 我正在使用 n 在 f 字符串中循环时更改页码,但我想我可以从 1 开始范围并将页面放入 f 字符串中。感谢您指出这一点。
标签: python beautifulsoup