查找 URL 的评分

我正在尝试创建一个包含 20 家银行评论的数据框,在以下代码中,我试图获得 20 位客户的评分值,但发现这很困难,因为我是新的 BeautifulSoup 和 Webscraping。


import pandas as pd

import requests

from bs4 import BeautifulSoup

url = 'https://www.bankbazaar.com/reviews.html'

page = requests.get(url)

print(page.text)

soup = BeautifulSoup(page.text,'html.parser')



 Rating = []

rat_elem = soup.find_all('span')

for rate in rat_elem:

    Rating.append(rate.find_all('div').get('value')) 


 print(Rating)


米琪卡哇伊
浏览 165回答 2
2回答

郎朗坤

我更喜欢使用 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']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python