【问题标题】:python- extract html table without lossing axis titlespython-在不丢失轴标题的情况下提取html表
【发布时间】:2013-03-11 18:59:36
【问题描述】:

第一季度。有什么方法可以从表中提取数据但仍然能够追踪轴标题? Q2。哪种方法会更好地从 html 表中提取数据? HTMLParser 或 beautifulsoup 还是其他?

我试图提取这个收入表 http://investing.businessweek.com/research/stocks/financials/financials.asp?ticker=TSCO:LN

我想成为

“以百万英镑计的货币”,"2009","2010","2011","2012"

“收入”、“53,898.0”、“56,910.0”、“60,455.0”、“64,539.0”

“总收入”、“53,898.0”、“56,910.0”、“60,455.0”、“64,539.0”

同时我想知道“56,910.0”是2009年的收入

但我遇到了两个问题:

  1. HTMLParser.HTMLParseError: 格式错误的开始标记,位于第 1148 行,第 47 列或 HTMLParser.HTMLParseError:错误的结束标记:“”,第 225 行,第 104 列

  2. 无法跟踪轴标题

非常感谢

【问题讨论】:

    标签: html python-2.7 html-parsing beautifulsoup extraction


    【解决方案1】:

    我已经做了很多抓取,BeautifulSoup 很少让人失望。

    
    from BeautifulSoup import BeautifulSoup 
    URL = "@987654321@"
    from urllib import urlopen
    HTML = urlopen ( URL )
    soup = BeautifulSoup ( HTML )
    statement = soup . find ( 'table', { 'class' : "financialStatement" } )
    rows = statement . findAll ( 'tr' )
    

    此时我想你会发现 rows 的长度为 25,它的第一项是表头,最后一项是所需表的最后一行。

    【讨论】:

    • 我确实安装了 BeautifulSoup,但出现了这个错误:ImportError: cannot import name BeautifulSoup
    • @user2158132 如果我没记错的话,BeautifulSoup 不是标准库的一部分。做pip install bs4(最新版)
    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    相关资源
    最近更新 更多