为了账号安全,请及时绑定邮箱和手机立即绑定

查找 URL 的评分

查找 URL 的评分

米琪卡哇伊 2021-11-16 09:55:41
我正在尝试创建一个包含 20 家银行评论的数据框,在以下代码中,我试图获得 20 位客户的评分值,但发现这很困难,因为我是新的 BeautifulSoup 和 Webscraping。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 = []rat_elem = soup.find_all('span')for rate in rat_elem:    Rating.append(rate.find_all('div').get('value'))  print(Rating)
查看完整描述

2 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

我更喜欢使用 CSS 选择器,因此您应该能够通过定位itemprop属性设置为的跨度来定位所有跨度ratingvalue。


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 = []

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']  

编辑:添加相关输出


查看完整回答
反对 回复 2021-11-16
?
呼唤远方

TA贡献1856条经验 获得超11个赞

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')


# 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']


查看完整回答
反对 回复 2021-11-16
  • 2 回答
  • 0 关注
  • 164 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信