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']
添加回答
舉報
