我如何使用 python 从 flashscore 中抓取足球结果 [英] How can i scrape a football results from flashscore using python

查看:10
本文介绍了我如何使用 python 从 flashscore 中抓取足球结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

' I am new to scraping. I want to scrape Premier League Season 2018-19 Results(fixtures, results, date), But i am struggling to navigate the web site. all i get is empty list / [None]. if you have a solution that you can share that will be a great help. '

'这是我尝试过的.'

'''

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.

You will have to install requests_html for my solution.

以下是我将如何去做:

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)

这会生成以下输出:

这篇关于我如何使用 python 从 flashscore 中抓取足球结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆