selenium 如何刮活火柴?

63lcw9qa  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(105)

我只需要从https://www.livescore.com/en/football/live/中抓取Live matches。下面的代码现在返回了可用足球比赛的完整列表(未开始、直播和结束),包括主队名称、客场名称和开始时间。我需要的是一个 Dataframe ,包括Live match、主队名称、客场名称和当前比赛分钟数。
THX

import requests
import pandas as pd
import datetime

url = "https://prod-public-api.livescore.com/v1/api/react/date/soccer/20220309/0.00?MD=1"
jsonData = requests.get(url).json()

rows = []
for stage in jsonData['Stages']:
    events = stage['Events']
    for event in events:
        gameDateTime = event['Esd']
        date_time_obj = datetime.datetime.strptime(str(gameDateTime), '%Y%m%d%H%M%S')
        gameTime = date_time_obj.strftime("%H:%M")
        homeTeam = event['T1'][0]['Nm']
        awayTeam = event['T2'][0]['Nm']
        
        row = {
            'Home':homeTeam,
            'Away':awayTeam,
            'Time':gameTime}
        rows.append(row)
        
df = pd.DataFrame(rows)
nzrxty8p

nzrxty8p1#

这只是从正确的端点提取数据的问题。
我添加了分数,但如果需要,您可以很容易地删除它。

    • 代码:**
import requests
import pandas as pd
import datetime

url = "https://prod-public-api.livescore.com/v1/api/react/live/soccer/0.00?MD=1"
jsonData = requests.get(url).json()

rows = []
for stage in jsonData['Stages']:
    events = stage['Events']
    for event in events:
        gameDateTime = event['Esd']
        date_time_obj = datetime.datetime.strptime(str(gameDateTime), '%Y%m%d%H%M%S')
        gameTime = date_time_obj.strftime("%H:%M")
        homeTeam = event['T1'][0]['Nm']
        homeScore = event['Tr1']
        
        awayTeam = event['T2'][0]['Nm']
        awayScore = event['Tr2']
        
        matchClock = event['Eps']
        
        row = {
            'Home':homeTeam,
            'Home Score':homeScore,
            'Away':awayTeam,
            'Away Score':awayScore,
            'Match Clock':matchClock,
}
        rows.append(row)
        
live_df = pd.DataFrame(rows)
    • 输出:**
print(live_df)
                  Home Home Score              Away Away Score Match Clock
0          RC Relizane          1         JS Saoura          2       90+1'
1            Urartu FC          0      FC Alashkert          0         63'
2         BATE Borisov          1   Torpedo Zhodino          1          FT
3      Ethnikos Achnas          0  Enosis Paralimni          0         24'
4        Hearts of Oak          0              WAFA          0         25'
5   PAE Veria NFC 2019          1        AE Larissa          1       90+1'
6             Arema FC          1    Persib Bandung          2          FT
7  Buducnost Podgorica          0              Zeta          0         23'
8               Zilina          1           Komarno          0         68'
9             NK Celje          1           Domzale          0         65'

相关问题