python爬取足球比赛信息(一)

爬取可购买的比赛信息

import datetime
import json
import time
import requests
import pymysql
from datetime import date, timedelta



url="https://webapi.sporttery.cn/gateway/jc/football/getMatchCalculatorV1.qry?poolCode=hhad,had&channel=c"

headers={
"Content-Type":" application/json"
}

def getdata():

    responese=requests.get(url,headers).text
    responese=json.loads(responese)
    all=len(responese['value']['matchInfoList'])
    for j in range(0,all):
        day1 = responese['value']['matchInfoList'][j]['businessDate']
        print(day1)
        dslen = len(responese['value']['matchInfoList'][j]['subMatchList'])
        for i in range(0,dslen):
            num=responese['value']['matchInfoList'][j]['subMatchList'][i]['matchNum']
            addname=responese['value']['matchInfoList'][j]['subMatchList'][i]['leagueAbbName']
            zhuname=responese['value']['matchInfoList'][j]['subMatchList'][i]['homeTeamAllName']
            kename=responese['value']['matchInfoList'][j]['subMatchList'][i]['awayTeamAbbName']
            lens=len(responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'])
            for z in range(0,lens):
                hh=responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][z]['poolCode']
                if z==0 and hh=="HHAD":
                    rasheng =responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][0]['a']
                    rhsheng=responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][0]['h']
                    rd=responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][0]['d']
                    rang1 = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][0]['goalLine']
                elif z==1 and hh=="HAD":
                    asheng = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][1]['a']
                    hsheng = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][1]['h']
                    d = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][1]['d']
                    rang = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][1]['goalLine']
                elif z==0 and hh=="HAD":
                    asheng =responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][0]['a']
                    hsheng=responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][0]['h']
                    d=responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][0]['d']
                    rang1 = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][0]['goalLine']
                elif z==1 and hh=="HHAD":
                    rasheng = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][1]['a']
                    rhsheng = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][1]['h']
                    rd = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][1]['d']
                    rang = responese['value']['matchInfoList'][j]['subMatchList'][i]['oddsList'][1]['goalLine']
                else:
                    break
            print("-------------"+addname+"-------------")
            print(num)
            print(zhuname + "-" + kename + "[0]   胜 " + hsheng + "--平 " + d + "--负 " + asheng)
            print(zhuname + "-" + kename + "["+rang+rang1+"]   胜 " + rhsheng + "--平 " + rd + "--负 " + rasheng)

爬取比赛结果

def amidithion():
    starttime=date.today() + timedelta(days = -1)
    endtime=time.strftime("%Y-%m-%d",time.localtime())
    url="https://webapi.sporttery.cn/gateway/jc/football/getMatchResultV1.qry?matchPage=1&pcOrWap=0&leagueId=&matchBeginDate="+str(starttime)+"&matchEndDate="+str(endtime)
    response=requests.get(url).text
    print(url)
    response=json.loads(response)
    lenght=len(response['value']['matchResult'])
    for i in range(0,lenght):
        day=str(starttime).replace("-","")
        num=str(response['value']['matchResult'][i]['matchNum'])
        score=response['value']['matchResult'][i]['sectionsNo999']
        win=response['value']['matchResult'][i]['winFlag']
        goalLine=response['value']['matchResult'][i]['goalLine']
        if score=="取消":
            continue
        if win=="A":
            win="负"
        elif win=="H":
            win="胜"
        else:
            win="平"
        a=int(score[2])
        if goalLine[0]=="+":
            h=int(score[0])+int(goalLine[1])
        else:
            h=int(score[0]) - int(goalLine[1])
        if a>h:
            rwin="让负"
        elif a<h:
            rwin="让胜"
        else:
            rwin="让平"
        id = str(day + num)
        print(num+",比分:"+score+"比赛结果:"+win+",让球结果:"+rwin)
可以使用 Python 的 requests 库和 BeautifulSoup 库来实现对足球比赛实时数据爬取。 首先,需要找到一个提供足球比赛实时数据的网站,并查看网站的 HTML 结构。一般来说,这类网站会提供一个包含实时数据的表格。 接下来,使用 requests 库向这个网站发送 HTTP 请求,获取网站的 HTML 内容。然后,使用 BeautifulSoup 库解析 HTML 内容,找到包含实时数据的表格。 在表格中找到射正球门这个数据项,并提取出在上下半场的数据。这里可以使用 BeautifulSoup 库提供的 find_all() 函数来实现。 最后,将提取出的数据保存到本地文件或者数据库中,每隔一段时间(例如 10 秒)重新执行一次上述步骤,实现实时监控。 下面是一个简单的示例代码,可以根据实际情况进行修改和优化: ``` import requests from bs4 import BeautifulSoup import time # 网站 URL url = "https://www.example.com/live-scores" while True: # 发送 HTTP 请求 response = requests.get(url) # 解析 HTML 内容 soup = BeautifulSoup(response.content, 'html.parser') # 找到包含实时数据的表格 table = soup.find('table', {'class': 'live-scores'}) # 找到射正球门这个数据项 shots_on_target = table.find_all('td', {'class': 'shots-on-target'}) # 提取上下半场的射正球门数据 first_half_shots_on_target = shots_on_target[0].text.strip() second_half_shots_on_target = shots_on_target[1].text.strip() # 打印结果 print("上半场射正球门数:", first_half_shots_on_target) print("下半场射正球门数:", second_half_shots_on_target) # 等待 10 秒 time.sleep(10) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值