1 回答

TA貢獻1790條經驗 獲得超9個贊
正如您所注意到的,您的數據采用 JSON 格式。它直接嵌入到頁面中的腳本標記中,因此很容易使用它美麗湯。然后你需要解析json以提取所有表(對應3個選項卡):
import requests
from bs4 import BeautifulSoup
import json
url = 'https://overwatchleague.com/en-us/standings'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
script = soup.find("script",{"id":"__NEXT_DATA__"})
data = json.loads(script.text)
tabs = [
i.get("standings")["tabs"]
for i in data["props"]["pageProps"]["blocks"]
if i.get("standings") is not None
]
result = [
{ i["title"] : i["tables"][0]["teams"] }
for i in tabs[0]
]
print(json.dumps(result, indent=4, sort_keys=True))
上面的代碼給你一個字典,鍵是3個選項卡的標題,值是表數據
- 1 回答
- 0 關注
- 94 瀏覽
添加回答
舉報