代码之家  ›  专栏  ›  技术社区  ›  Steve

在标记数据ID上使用python从espn提取数据

  •  0
  • Steve  · 技术社区  · 6 年前

    我正在寻找使用python从espn网站上获取一些数据。

    http://www.espn.co.uk/rugby/playerstats?gameId=293905&league=289234

    我正在使用BeautifulSoup从页面中提取内容。

     item = soup.findAll('span', attrs={'data-reactid': '136'})[0].text
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   keithpjolley    6 年前

    这个 beautifulsoup 这条路看起来很难走。我认为这可能对你有用:

    import requests
    import json
    import re
    
    url = "http://www.espn.co.uk/rugby/playerstats?gameId=293905&league=289234"
    html_doc = requests.get(url)
    # not the best regex but it works. there's a lot of data.
    stats = json.loads(re.search(r"window.__INITIAL_STATE__\s*=\s*({.*});",html_doc.text).group(1))
    
    # show what we have
    stats['gamePackage']['matchLineUp'].keys()                                                                                     
    # Out[42]: dict_keys(['text', 'home', 'away', 'gameState', 'sport', 'show'])
    
    # no idea what this sport is. a typo?
    stats['gamePackage']['matchLineUp']['sport']                                                                                        
    # Out[43]: 'rugby'
    
    stats['gamePackage']['matchLineUp']['home']                                                                                        
    # {'name': 'ITALY',
    #  'logo': 'http://a1.espncdn.com/combiner/i?img=/i/teamlogos/rugby/teams/500/20.png&h=35&w=35',
    #  'team': [
    #   {'id': '91554',
    #    'url': 'http://en.espn.co.uk/sport/rugby/player/91554.html',
    #    'name': 'Jayden Hayward',
    #    'number': '15',
    #    'position': 'FB',
    #    'captain': False,
    #    'subbed': False,
    #    'homeAway': 'home',
    #    ...
    

    for home_player in stats['gamePackage']['matchLineUp']['home']['team']: 
       print("{} - {}".format(home_player['name'], home_player['number'])) 
    Jayden Hayward - 15
    Tommaso Benvenuti - 14
    Michele Campagnaro - 13
    Tommaso Castello - 12
    Luca Sperandio - 11
    Tommaso Allan - 10
    Tito Tebaldi - 9
    Andrea Lovotti - 1
    Leonardo Ghiraldini - 2
    Simone Ferrari - 3
    Alessandro Zanni - 4
    Dean Budd - 5
    Sebastian Negri - 6
    Jake Polledri - 7
    Braam Steyn - 8
    

    里面有很多其他的信息,但我想这会让你走。。。