【问题标题】:Scrape ESPN Current week NFL lines into a pandas dataframe将 ESPN 当前周的 NFL 行刮到 pandas 数据框中
【发布时间】:2021-10-08 17:49:58
【问题描述】:

以下代码仅返回第一个游戏。我想将所有第 5 周的游戏和台词放入一个数据框中。提前致谢。

import pandas as pd # library for data analysis
import requests # library to handle requests
from bs4 import BeautifulSoup # library to parse HTML documents

# get the response in the form of html
url="https://www.espn.com/nfl/lines"
response=requests.get(url)

# parse data from the html into a beautifulsoup object
soup = BeautifulSoup(response.text,'html.parser')
indiatable=soup.find('section',{'class':"Card"})

df=pd.read_html(str(indiatable))
# convert list to dataframe
df=pd.DataFrame(df[0])
print(df.head())

df

           9:30 AM    REC (ATS)  LINE  OPEN   ML    FPI
0    New York Jets  1-3 (1-3-0)  45.0  43.5  130  42.8%
1  Atlanta Falcons  1-3 (1-3-0)  -2.5  -2.5 -150  56.9%
Out[85]:
9:30 AM REC (ATS)   LINE    OPEN    ML  FPI
0   New York Jets   1-3 (1-3-0) 45.0    43.5    130 42.8%
1   Atlanta Falcons 1-3 (1-3-0) -2.5    -2.5    -150    56.9%

【问题讨论】:

    标签: python pandas dataframe beautifulsoup espn


    【解决方案1】:

    可以使用 API 调用获取您要查找的数据。

    只需遍历响应并构建 df(s)

    见下文

    import requests
    
    url = 'https://site.web.api.espn.com/apis/v2/scoreboard/header?sport=football&league=nfl&region=us&lang=en&contentorigin=espn&buyWindow=1m&showAirings=buy%2Clive%2Creplay&showZipLookup=true&tz=America/New_York'
    
    r = requests.get(url)
    if r.status_code == 200:
      print(r.json())
    else:
      print(f'Oops - status code is {r.status_code}')
    

    【讨论】:

    • requests.get('https://site.web.api.espn.com/apis/v2/scoreboard/header', params={'sport': 'football', 'league': 'nfl', 'region': 'us', 'lang': 'en', 'contentorigin': 'espn', 'buyWindow': '1m', 'showAirings': 'buy,live,replay', 'showZipLookup': 'true', 'tz': 'America/New_York'})
    【解决方案2】:

    您只能使用 pandas:

    dfs = pd.read_html("https://www.espn.com/nfl/lines")
    

    dfs - 数据帧列表

    在单个 DataFrame 上合并:

    df = pd.concat(dfs)
    

    【讨论】:

      猜你喜欢
      • 2019-06-01
      • 2018-11-17
      • 2020-10-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多