[英]Beautiful Soup not parsing all tags
我附上了我要抓取的網站的 HTML 的屏幕截圖,有一個表格,在其中我想從正文中的行中獲取一些數據(它們顯然存在),但是,它不是工作所以我決定打印 tbody ,這表明解析找到了 table 和 tbody 但沒有找到其中的行。 我不知道如何解決這個問題,任何幫助將不勝感激。 來自 wesbite 的 HTML
這是我的代碼的開頭:
url = "https://superfpl.com/player_stats"
results = requests.get(url)
soup = BeautifulSoup(results.text, "html.parser")
players = []
teams = []
positions = []
ownerships = []
print(soup.find("tbody"))
player_div = soup.find_all('tr', role_="row", class_="odd")
數據通過 JavaScript 動態加載。 但是您可以使用requests
模塊來模擬它:
import requests
url = 'https://superfpl.com/ajax/player_stats'
data = requests.get(url).json()
# uncomment this to see all data (WARNING, huge list!):
# import json
# print(json.dumps(data, indent=4))
# pretty print some data to screen:
for row in data['data']:
print('{:<20}{:<20}{:<20}'.format(row['web_name'], row['position'], row['points_per_game']))
印刷:
Connolly FWD 2.4
Cresswell DEF 2.7
Lennon MID 0.9
Mooy MID 2.9
Ramsdale GKP 3.4
Wan-Bissaka DEF 3.3
Koiki DEF 0.0
Doucouré MID 3.3
Idah FWD 1.3
Lallana MID 1.8
Masina DEF 2.8
Adam Smith DEF 2.2
... and so on.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.