【问题标题】:Scraping opening odds刮开赔率
【发布时间】:2021-11-12 12:53:11
【问题描述】:

我想从这个网站上获取某个博彩公司的开盘赔率:https://www.betexplorer.com/match-odds/4YWZmAJt/1/1x2/ 所以我有这个功能:

def get_odds(ids):
  for id in ids:
    url = f'https://www.betexplorer.com{id}'
    matchup = url.split('/')[6]
    match_id = url.split('/')[7]  # <-- this is the last part of URL
    api_url = "https://www.betexplorer.com/match-odds/{}/1/1x2/".format(match_id)
    headers = {"Referer": "https://www.betexplorer.com",
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'}
    s = requests.Session()
    s.headers.update(headers)
    response = s.get(api_url, headers=headers)
    soup = BeautifulSoup(response.text,'html.parser')
    print(soup)

ids 是一个list,带有这种字符串

ids=['/soccer/argentina/primera-division-2016/gimnasia-l-p-colon-santa-fe/4YWZmAJt/']

我想以博彩公司的名义find并刮掉他的data-opening-odd

【问题讨论】:

    标签: python web-scraping beautifulsoup python-requests


    【解决方案1】:

    进行了一些调试,但响应是一个字典,唯一的键是“odds”。值是html。您将在下面看到我将值放入 bs4 解析器的位置。

    赔率在表格中,所以我找到所有并循环遍历。第一个和最后一个元素似乎无关紧要,因此您会在 for 循环中看到 [1:-1]。

    def get_odds(ids):
      for id in ids:
        url = f'https://www.betexplorer.com{id}'
        matchup = url.split('/')[6]
        match_id = url.split('/')[7]  # <-- this is the last part of URL
        api_url = "https://www.betexplorer.com/match-odds/{}/1/1x2/".format(match_id)
        headers = {"Referer": "https://www.betexplorer.com",
                        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'}
        s = requests.Session()
        s.headers.update(headers)
        response = s.get(api_url, headers=headers)
        soup = BeautifulSoup(response.json()['odds'],'html.parser')
        # print(soup)
        return soup
    
    ids=['/soccer/argentina/primera-division-2016/gimnasia-l-p-colon-santa-fe/4YWZmAJt/']
    
    for id in ids:
        soup = get_odds(ids)
        trs = soup.find_all('tr')
        for tr in trs[1:-1]:
            print(tr.find(class_="in-bookmaker-logo-link").text)
            print(tr.find( class_="table-main__detail-odds").get('data-opening-odd'))
    
    10Bet
    2.00
    188BET
    2.00
    1xBet
    1.92
    888sport
    1.83
    bet-at-home
    1.89
    bet365
    2.00
    Betfair
    2.00
    Betsafe
    2.00
    Betsson
    2.00
    BetVictor
    1.95
    Betway
    2.00
    bwin
    1.90
    ComeOn
    2.00
    Interwetten
    1.95
    Pinnacle
    2.06
    SBOBET
    2.05
    Unibet
    1.95
    William Hill
    2.00
    youwin
    1.85
    Betfair Exchange
    1.02
    

    【讨论】:

    • 很好用,我添加了另一个 for 循环来获得所有可能性。
    猜你喜欢
    • 2020-10-01
    • 2020-10-10
    • 1970-01-01
    • 2011-02-17
    • 2013-05-23
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多