【问题标题】:Scrape a UEFA web page using Beautifulsoup使用 Beautifulsoup 抓取 UEFA 网页
【发布时间】:2019-08-08 17:50:01
【问题描述】:

我想抓取一个 URL。我已经识别出源代码中的数据,但是.find_all()的结果是None

要抓取的示例 URL: https://fr.uefa.com/uefaeuropaleague/season=2020/matches/round=2001148/match=2028066/statistics/index.html?iv=true

示例 HTML:

<div class="match-statistics--goals-scored stats-visualization--horizontal-bar match-statistics--item">
      <div class="goals-scored--values-wrap match-statistics--values-wrap">
        <div class="goals-scored--value graph-bar--number-value graph-bar--number-value__home-team" data-bind="text: homeGoalsScored">1</div>
        <div class="goals-scored--value graph-bar--number-value graph-bar--number-value__away-team" data-bind="text: awayGoalsScored">0</div>
        <div class="goals-scored--title match-statistics--sub-title">
          Buts marqués
        </div>
      </div>
      <!-- ko let: { width: 'width:' + (homeGoalsScored + awayGoalsScored > 0 ? (homeGoalsScored * 100 / (homeGoalsScored + awayGoalsScored)) : 0) + '%', classes: 'goals-scored-graph-bar graph-bar' + (homeGoalsScored + awayGoalsScored === 0 ? ' graph-bar__zero' : '' ) } -->
      <div class="goals-scored-graph-bar graph-bar" data-bind="attr: { class: classes }">
        <div class="goals-scored-graph-bar__value graph-bar__value" data-bind="attr: { style: width }" style="width:100%"></div>
      </div>
      <!-- /ko -->
    </div>

我的代码:


  req = urllib.request.Request(
      link, 
      data=None, 
      headers={
          'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
      }
      )
    matchs = []
    with urllib.request.urlopen(req) as urlpage:
        html = urlpage.read().decode()
        soup = BeautifulSoup.BeautifulSoup(html,"html.parser")  
        stats = soup.find_all("div",class_='match-statistics--item')

但是stats 是空的。

我也尝试过使用全类作为选择器:

soup.find_all("div",class_='match-statistics--goals-scored stats-visualization--horizontal-bar match-statistics--item')

我只想获取所有统计信息,例如:

'Home','TOTAL DE TIRS',21
'Away','TOTAL DE TIRS',6
'Home','CADRÉS',6
'Away','CADRÉS',3
....

【问题讨论】:

    标签: web-scraping beautifulsoup


    【解决方案1】:

    数据是从您可以在网络选项卡中找到的 API 动态加载的。有不同长度的项目,所以我使用 itertools 来确保我们不打印不存在 home 或 away 项目的地方

    import requests, itertools
    
    r = requests.get('https://digital-api.uefa.com/v1/matches/2028066/statistics/team?language=FR').json()
    home = {i['typeDisplayName']:i['value'] for i in r['homeTeam']['statistics']}
    away = {i['typeDisplayName']:i['value'] for i in  r['awayTeam']['statistics']}
    
    for item in itertools.zip_longest(home.keys(), away.keys(), fillvalue=None):
        if item[0]:
            print(', '.join(['Home', item[0], str(home[item[0]])]))
        if item[1]:
            print(', '.join(['Away', item[1], str(away[item[1]])]))
    

    示例输出:

    【讨论】:

    • 非常感谢,我尝试使用 api 找到这种链接,但我错过了这个
    猜你喜欢
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多