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']
编辑:添加相关输出
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']
添加回答
举报