郎朗坤
我更喜欢使用 CSS 选择器,因此您应该能够通过定位itemprop属性设置为的跨度来定位所有跨度ratingvalue。import pandas as pdimport requestsfrom bs4 import BeautifulSoupurl = 'https://www.bankbazaar.com/reviews.html'page = requests.get(url)print(page.text)soup = BeautifulSoup(page.text,'html.parser')Rating = []for rate in soup.select('span[itemprop=ratingvalue]'): Rating.append(rate.get_text()) print(Rating)相关输出['4.0', '5.0', '5.0', '5.0', '4.0', '4.0', '5.0', '5.0', '5.0', '5.0', '4.0', '5.0', '5.0', '5.0', '5.0', '4.0', '4.5', '4.0', '4.0', '4.0'] 编辑:添加相关输出
呼唤远方
import pandas as pdimport requestsfrom bs4 import BeautifulSoupurl = 'https://www.bankbazaar.com/reviews.html'page = requests.get(url)print(page.text)soup = BeautifulSoup(page.text,'html.parser')# Find all the span elements where the "itemprop" attribute is "ratingvalue". Rating = [item.text for item in soup.find_all('span', attrs={"itemprop":"ratingvalue"})]print(Rating)# The output# ['4.0', '5.0', '5.0', '5.0', '4.0', '4.0', '5.0', '5.0', '5.0', '5.0', '4.0', '5.0', '5.0', '5.0', '5.0', '4.0', '4.5', '4.0', '4.0', '4.0']