pandas 请求无法找到表Python

42fyovps  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(110)

我正在尝试以Python数据框的形式抓取三振出局赔率数据here
下面是我的代码:

games = "[https://www.fantasypros.com/mlb/schedules/](https://www.rotowire.com/betting/mlb/player-props.php)"

page = requests.get(games)

games_frame = pd.read_html(page.text, displayed_only=False)\[1\]
sirbozc5

sirbozc51#

尝试:

import re
import json
import requests
import pandas as pd
from bs4 import BeautifulSoup

url = 'https://www.rotowire.com/betting/mlb/player-props.php'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

script_tag = soup.find(lambda tag: tag.name == 'script' and tag.text and '"Strikeouts"' in tag.text)

data = re.search(r"data:\s*(\[.*\])", script_tag.text)
data = json.loads(data.group(1))

df = pd.DataFrame(data)
print(df.head())

图纸:

gameID playerID firstName lastName             name team   opp                                                                  logo                                      playerLink draftkings_strikeouts draftkings_strikeoutsUnder draftkings_strikeoutsOver fanduel_strikeouts fanduel_strikeoutsUnder fanduel_strikeoutsOver mgm_strikeouts mgm_strikeoutsUnder mgm_strikeoutsOver pointsbet_strikeouts pointsbet_strikeoutsUnder pointsbet_strikeoutsOver
0  2512637    12854     Jacob   deGrom     Jacob deGrom  TEX    KC  https://content.rotowire.com/images/teamlogo/baseball/100TEX.png?v=6     /betting/mlb/player/jacob-degrom-odds-12854                   9.5                       -140                       105                9.5                    -138                    108           None                None               None                 None                      None                     None
1  2511983    12739    Shohei   Ohtani    Shohei Ohtani  LAA   WAS  https://content.rotowire.com/images/teamlogo/baseball/100LAA.png?v=6    /betting/mlb/player/shohei-ohtani-odds-12739                   7.5                       -150                       105                7.5                    -122                   -106           None                None               None                 None                      None                     None
2  2512064    10510    Gerrit     Cole      Gerrit Cole  NYY  @CLE  https://content.rotowire.com/images/teamlogo/baseball/100NYY.png?v=6      /betting/mlb/player/gerrit-cole-odds-10510                   6.5                        105                      -135                6.5                     108                   -136           None                None               None                 None                      None                     None
3  2513620    14482  Cristian   Javier  Cristian Javier  HOU  @PIT  https://content.rotowire.com/images/teamlogo/baseball/100HOU.png?v=6  /betting/mlb/player/cristian-javier-odds-14482                   6.5                       -110                      -120                6.5                    -124                   -102           None                None               None                 None                      None                     None
4  2512307    15116     Pablo    Lopez      Pablo Lopez  MIN   CWS  https://content.rotowire.com/images/teamlogo/baseball/100MIN.png?v=6      /betting/mlb/player/pablo-lopez-odds-15116                   6.5                                                                     6.5                    -158                    124           None                None               None                 None                      None                     None

相关问题