记录一个爬取足球赔率信息的一个脚本,方便平时抓取网站直接使用
class getLeagueOdds():
def __init__(self,
leagueUrl,
seasonName,
leagueId,
header = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"}
):
self.leagueUrl = leagueUrl
self.seasonName = seasonName
self.leagueId = leagueId
self.header = header
# 获取指定轮数数据
def get_matchId(self, roundNum):
interfer_url = 'https://sport.ttyingqiu.com/sportdata/f'
data = '{"leagueId":' + str(self.leagueId) + ',"pageNo":1,"pageSize":100,"round":"' + str(roundNum) + '","seasonFlag":0,"seasonName":"' + str(self.seasonName) + '","apiName":"getLeagueMatchList"}'
response = requests.get(interfer_url, data = data, headers=self.header).text
print(json.loads(response))
res_list = json.loads(response)['matchList']
match_list = [i['qtMatchId'] for i in res_list]
return match_list
# 获取赛季全部数据
def get_season_all_matchId(self):
matchId_bag = []
print('####开始循环获取全部比赛ID')
for i in trange(1, 100):
print(i)
try:
matchId_bag.extend(self.get_matchId(i))
except:
break
return matchId_bag
# 获取每场比赛的赔率
def get_match_odd(self):
matchId_bag = get_season_all_matchId()
for i in matchId_bag:
match_detail_url = 'https://www.ttyingqiu.com/live/zq/matchDetail/dxq/' + str(i)
response = requests.get(match_detail_url, headers=self.header).text
tree = etree.HTML(response)
img_lst = tree.xpath('//*[@id="app"]/div[2]/div[2]/div[2]/div[2]/table[1]/tbody/tr[1]/td[8]/a[3]/@href')
if __name__ == '__main__':
dem = getLeagueOdds(leagueUrl = '', seasonName = "2021-2022", leagueId = 2079)
dem.get_matchId(roundNum = 1)
dem.get_season_all_matchId()
|