【问题标题】:Get link text from HTML using beautifulsoup使用 beautifulsoup 从 HTML 中获取链接文本
【发布时间】:2015-01-03 06:49:45
【问题描述】:

从下面的 HTML 示例部分,我使用 beautifilsoup 从页面中提取了一堆足球比分,很简单:

<tr class='report' id='match-row-EFBO695086'> <td class='statistics show' title='Show latest      match stats'> <button>Show</button> </td>  <td class='match-competition'> Premier League  </td>  <td class='match-details
teams'> <p> <span class='team-home teams'> <a href='/sport/football/teams/manchester-city'>Man City</a> </span>   <span class='score'> <abbr title='Score'> 1-0 </abbr> </span>   <span class='team-away teams'> <a
href='/sport/football/teams/crystal-palace'>Crystal Palace</a> </span>   </p> </td> <td class="match-date"> Sat 28 Dec </td>   <td class='time'>  Full time  </td>   <td class='status'>    <a class='report'
href='/sport/football/25474625'>Report</a>

from bs4 import BeautifulSoup
import urllib.request
import csv

url = 'http://www.bbc.co.uk/sport/football/teams/manchester-city/results/'
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page)

for score in soup.findAll('abbr'):
    print(score.string)

*** Remote Interpreter Reinitialized  ***
>>> 
None
1-2 
1-0 
0-2 
2-1 
2-2 
4-1 
0-2 
1-1 

如何从这部分 HTML 中提取团队名称:

<span class='team-away teams'> <a href='/sport/football/teams/crystal-palace'>Crystal Palace</a>    </span> 

【问题讨论】:

    标签: python html web-scraping beautifulsoup html-parsing


    【解决方案1】:

    这个想法是首先获取包含每个游戏信息的元素 - 这些是带有class="report"tr 标签。对于每一行,按班级team-hometeam-away 获取团队名称,并按标签名称abbr 得分:

    from bs4 import BeautifulSoup
    import urllib.request
    
    url = 'http://www.bbc.co.uk/sport/football/teams/manchester-city/results/'
    page = urllib.request.urlopen(url)
    soup = BeautifulSoup(page)
    
    for match in soup.select('table.table-stats tr.report'):
        team1 = match.find('span', class_='team-home')
        team2 = match.find('span', class_='team-away')
        score = match.abbr
        if not all((team1, team2, score)):
            continue
    
        print(team1.text, score.text, team2.text)
    

    打印:

    Man City   1-2   CSKA 
    Man City   1-0   Man Utd 
    Man City   0-2   Newcastle 
    West Ham   2-1   Man City 
    ...
    

    仅供参考,table.table-stats tr.report 是一个CSS Selector,它匹配table 内的所有tr 标记和class="report"class="table-stats"

    【讨论】:

    • 很棒的解释和示例,当您在参考原始 HTML 的同时看到代码示例时,更容易理解它是如何工作的。太棒了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2020-05-19
    • 2016-03-25
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多