python爬虫(request+xpath的简单demo模板)

记录一个爬取足球赔率信息的一个脚本,方便平时抓取网站直接使用

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()

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

'Humz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值