gpt4 book ai didi

python-3.x - 我如何使用 python 从 flashscore 中抓取足球结果

转载 作者:行者123 更新时间:2023-12-05 08:50:38 24 4
gpt4 key购买 nike

网络抓取 Python

' 我是新手。我想抓取英超联赛 2018-19 赛季的结果(赛程、结果、日期),但我正在努力浏览网站。我得到的只是空列表/[无]。如果您有可以分享的解决方案,那将是一个很大的帮助。 '

“这是我试过的。”

'''

import pandas as pd
import requests as uReq
from bs4 import BeautifulSoup

url = uReq.get('https://www.flashscore.com/football/england/premier-league-2018-2019/results/')

soup = BeautifulSoup(url.text, 'html.parser')

divs = soup.find_all('div', attrs={'id': 'live-table'})

Home = []
for div in divs:
anchor = div.find(class_='event__participant event__participant--home')

Home.append(anchor)

print(Home)

'''

最佳答案

您必须为我的解决方案安装 requests_html

下面是我将如何去做:

from requests_html import AsyncHTMLSession
from collections import defaultdict
import pandas as pd


url = 'https://www.flashscore.com/football/england/premier-league-2018-2019/results/'

asession = AsyncHTMLSession()

async def get_scores():
r = await asession.get(url)
await r.html.arender()
return r

results = asession.run(get_scores)
results = results[0]

times = results.html.find("div.event__time")
home_teams = results.html.find("div.event__participant.event__participant--home")
scores = results.html.find("div.event__scores.fontBold")
away_teams = results.html.find("div.event__participant.event__participant--away")
event_part = results.html.find("div.event__part")


dict_res = defaultdict(list)

for ind in range(len(times)):
dict_res['times'].append(times[ind].text)
dict_res['home_teams'].append(home_teams[ind].text)
dict_res['scores'].append(scores[ind].text)
dict_res['away_teams'].append(away_teams[ind].text)
dict_res['event_part'].append(event_part[ind].text)

df_res = pd.DataFrame(dict_res)

这会生成以下输出:

enter image description here

关于python-3.x - 我如何使用 python 从 flashscore 中抓取足球结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61406351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com