事實上,在這種情況下事情並不那麼複雜。當您單擊「全部」時,不會發出網絡請求。所有數據已經存在 - 在HTML中的script
標記內,您只需要提取它。
使用requests
(下載網頁內容),BeautifulSoup
(解析HTML和定位所需script
元件),re
(提取從腳本期望的「播放器」陣列)和json
(加載陣列工作碼字符串轉換成Python列表):
import json
import re
import requests
from bs4 import BeautifulSoup
url = "https://rotogrinders.com/game-stats/nhl-skater?site=draftkings&date=11-22-2016"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
pattern = re.compile(r"var data = (\[.*?\]);$", re.MULTILINE | re.DOTALL)
script = soup.find("script", text=pattern)
data = pattern.search(script.text).group(1)
data = json.loads(data)
# printing player names for demonstration purposes
for player in data:
print(player["player"])
版畫: 「也許某些庫,將允許按鈕點擊」
Jeff Skinner
Jordan Staal
...
William Carrier
A.J. Greer
硒。 –
到目前爲止,你做了什麼,如果你顯示一些代碼或試圖做任務的人更願意幫助。 – Dalvenjia