【问题标题】:Python HTML table scrape (Print)Python HTML 表格抓取(打印)
【发布时间】:2015-12-12 11:55:33
【问题描述】:

我一直在尝试用 Python 抓取 HTML 表格,但由于某种原因无法打印,请耐心等待,因为我刚刚开始使用 Python(2 天后)而且我几乎没有触及表面,这也是我的第一篇 Stack Overflow 帖子,所以我会尽量使其具有描述性。

很确定以前可能有人问过这个问题,在这种情况下我很抱歉。

无论如何!代码如下:

import urllibs2
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen ('http://premierleague.com/en-gb/matchday/league-table.html').read())
for row in soup('table',{'class':'leagueTable'})[0].tbody('tr'):
tds=row('td')

http://premierleague.com/en-gb/matchday/league-table.html

我在 Python 方面很弱,我不确定代码是否适合这种类型的抓取,但据我自己的理解,这是我无法工作的打印。我尝试了不同的打印方式,但无法正常工作。

【问题讨论】:

    标签: python web-scraping beautifulsoup html-table


    【解决方案1】:

    使其更简单 - 使用 CSS selector 获取所需的行 - 具有 club-row 类的 tr 元素位于具有 leagueTable 类的 table 内。对于每一行,获取所有单元格的文本。工作示例:

    import urllib2
    
    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(urllib2.urlopen('http://www.premierleague.com/en-gb/matchday/league-table.html'))
    
    for row in soup.select("table.leagueTable tr.club-row"):
        cells = [cell.get_text(strip=True) for cell in row.find_all('td')]
        print cells
    

    打印:

    [u'1', u'', u'(1)', u'Manchester City', u'5', u'5', u'0', u'0', u'11', u'0', u'11', u'15']
    [u'2', u'', u'(2)', u'Leicester City', u'5', u'3', u'2', u'0', u'11', u'7', u'4', u'11']
    [u'3', u'', u'(3)', u'Manchester United', u'5', u'3', u'1', u'1', u'6', u'3', u'3', u'10']
    [u'4', u'', u'(4)', u'Arsenal', u'5', u'3', u'1', u'1', u'5', u'3', u'2', u'10']
    [u'5', u'', u'(10)', u'West Ham United', u'5', u'3', u'0', u'2', u'11', u'6', u'5', u'9']
    [u'6', u'', u'(5)', u'Crystal Palace', u'5', u'3', u'0', u'2', u'8', u'6', u'2', u'9']
    [u'7', u'', u'(6)', u'Everton', u'5', u'2', u'2', u'1', u'8', u'5', u'3', u'8']
    [u'8', u'', u'(7)', u'Swansea City', u'5', u'2', u'2', u'1', u'7', u'5', u'2', u'8']
    [u'9', u'', u'(8)', u'Norwich City', u'5', u'2', u'1', u'2', u'8', u'9', u'-1', u'7']
    [u'10', u'', u'(9)', u'Liverpool', u'5', u'2', u'1', u'2', u'3', u'6', u'-3', u'7']
    [u'11', u'', u'(11)', u'Southampton', u'5', u'1', u'3', u'1', u'5', u'5', u'0', u'6']
    [u'12', u'', u'(12)', u'Tottenham Hotspur', u'5', u'1', u'3', u'1', u'4', u'4', u'0', u'6']
    [u'13', u'', u'(13)', u'Watford', u'5', u'1', u'3', u'1', u'3', u'4', u'-1', u'6']
    [u'14', u'', u'(14)', u'West Bromwich Albion', u'5', u'1', u'2', u'2', u'3', u'6', u'-3', u'5']
    [u'15', u'', u'(15)', u'Aston Villa', u'5', u'1', u'1', u'3', u'6', u'8', u'-2', u'4']
    [u'16', u'', u'(16)', u'Bournemouth', u'5', u'1', u'1', u'3', u'6', u'9', u'-3', u'4']
    [u'17', u'', u'(17)', u'Chelsea', u'5', u'1', u'1', u'3', u'7', u'12', u'-5', u'4']
    [u'18', u'', u'(19)', u'Stoke City', u'5', u'0', u'2', u'3', u'3', u'7', u'-4', u'2']
    [u'19', u'', u'(20)', u'Sunderland', u'5', u'0', u'2', u'3', u'6', u'11', u'-5', u'2']
    [u'20', u'', u'(18)', u'Newcastle United', u'5', u'0', u'2', u'3', u'2', u'7', u'-5', u'2']
    

    现在我们可以清楚地看到 - 这对切尔西来说是一个糟糕的开端。

    【讨论】:

    • 干杯伙伴!不能让它打印超过 4 行,讽刺的是阿森纳。哈哈,希望切尔西能保持这种状态。另外,如何在 HTML 表中包含此代码,我的意思是这是一个简单的过程还是应该阅读它?提前谢谢兄弟!
    • @smokeyblunts 我们正在体验 bs4 在当前 python 环境中自动选择的differences between parsers。安装html5liblxml 并调用`BeaufitulSoup` 构造函数为BeautifulSoup(urllib2.urlopen('http://premierleague.com/en-gb/matchday/league-table.html'), "html5lib")BeautifulSoup(urllib2.urlopen('http://premierleague.com/en-gb/matchday/league-table.html'), "lxml")
    • 你好,alecxe,从来没有真正解决过解析问题,很抱歉再次打扰你。正如我之前提到的,我设法让 4 行工作,昨天没有太多时间,所以我想我今天就让它工作。但是,今天什么也没打印,代码和昨天完全一样,但是它没有打印一行,本来想给你发信息的,但我似乎找不到是否有 PM 功能.再次感谢您!
    • @smokeyblunts 当然,你需要修复你的 url - 它应该是http://www.premierleague.com/en-gb/matchday/league-table.html(注意那里的www
    猜你喜欢
    • 2018-02-24
    • 2021-07-08
    • 2016-01-31
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    相关资源
    最近更新 更多