将HTML表从给定的URL刮到CSV

时间:2010-04-09 22:40:47

标签: html language-agnostic parsing csv screen-scraping

我寻找一个可以在命令行上运行的工具,如下所示:

tablescrape 'http://someURL.foo.com' [n]

如果未指定n且页面上有多个HTML表格,则应在编号列表中对它们进行汇总(标题行,总行数)。 如果指定了n或者只有一个表,它应该解析表并将其作为CSV或TSV吐出到stdout。

潜在的附加功能:

  • 非常想要你可以解析表格中的表格,但是出于我的目的 - 从维基百科页面等中获取数据 - 这太过分了。
  • asciify任何unicode的选项。
  • 应用任意正则表达式替换以修复已解析表中的奇怪现象的选项。

你会用什么来拼凑这样的东西? Perl模块HTML::TableExtract可能是一个很好的起点,甚至可以处理嵌套表的情况。 这也可能是一个非常短的Python脚本BeautifulSoupYQL会是一个很好的起点吗? 或者,理想情况下,你有类似的东西写一个指针吗? (我肯定不是第一个需要这个的人。)

相关问题:

2 个答案:

答案 0 :(得分:13)

这是我的第一次尝试:

http://yootles.com/outbox/tablescrape.py

它需要更多的工作,比如更好的asciifying,但它是可用的。例如,如果您将其指向此list of Olympic records

./tablescrape http://en.wikipedia.org/wiki/List_of_Olympic_records_in_athletics

它告诉你有8个表可用,很明显第二个和第三个(男人和女人的记录)是你想要的:

1: [  1 cols,   1 rows] Contents 1 Men's rec
2: [  7 cols,  25 rows] Event | Record | Name | Nation | Games | Date | Ref
3: [  7 cols,  24 rows] Event | Record | Name | Nation | Games | Date | Ref
[...]

然后,如果你再次运行它,要求第二个表,

./tablescrape http://en.wikipedia.org/wiki/List_of_Olympic_records_in_athletics 2

您可以获得合理的明文数据表:

100 metres | 9.69 | Usain Bolt | Jamaica (JAM) | 2008 Beijing | August 16, 2008 | [ 8 ]
200 metres | 19.30 | Usain Bolt | Jamaica (JAM) | 2008 Beijing | August 20, 2008 | [ 8 ]
400 metres | 43.49 | Michael Johnson | United States (USA) | 1996 Atlanta | July 29, 1996 | [ 9 ]
800 metres | 1:42.58 | Vebjørn Rodal | Norway (NOR) | 1996 Atlanta | July 31, 1996 | [ 10 ]
1,500 metres | 3:32.07 | Noah Ngeny | Kenya (KEN) | 2000 Sydney | September 29, 2000 | [ 11 ]
5,000 metres | 12:57.82 | Kenenisa Bekele | Ethiopia (ETH) | 2008 Beijing | August 23, 2008 | [ 12 ]
10,000 metres | 27:01.17 | Kenenisa Bekele | Ethiopia (ETH) | 2008 Beijing | August 17, 2008 | [ 13 ]
Marathon | 2:06:32 | Samuel Wanjiru | Kenya (KEN) | 2008 Beijing | August 24, 2008 | [ 14 ]
[...]

答案 1 :(得分:1)

使用TestPlan我制作了一个粗略的脚本。鉴于Web表的复杂性,它可能需要在所有站点上进行定制。

第一个脚本列出了页面上的表格:

# A simple table scraping example. It lists the tables on a page
#
# Cmds.Site = the URL to scan
default %Cmds.Site% http://en.wikipedia.org/wiki/List_of_Olympic_records_in_athletics
GotoURL %Cmds.Site%

set %Count% 1
foreach %Table% in (response //table)
    Notice Table #%Count%
    # find a suitable name, look back for a header
    set %Check% ./preceding::*[name()='h1' or name()='h2' or name()='h3'][1]
    if checkIn %Table% %Check%
        Notice (selectIn %Table% %Check%)
    end

    set %Count% as binOp %Count% + 1
end

然后,第二个脚本将一个表的数据提取到CSV文件中。

# Generic extract of contents of a table in a webpage
# Use list_tables to get the list of table and indexes
#
# Cmds.Site = the URL to scan
# Cmds.Index = Table index to scan
default %Cmds.Site% http://en.wikipedia.org/wiki/List_of_Olympic_records_in_athletics
default %Cmds.Index% 2

GotoURL %Cmds.Site%

set %Headers% //table[%Cmds.Index%]/tbody/tr[1]
set %Rows% //table[%Cmds.Index%]/tbody/tr[position()>1]

# Get an cleanup the header fields 
set %Fields% withvector
end
foreach %Header% in (response %Headers%/*)
    putin %Fields% (trim %Header%)
end
Notice %Fields%

# Create an output CSV
call unit.file.CreateDataFile with
    %Name% %This:Dir%/extract_table.csv
    %Format% csv
    %Fields% %Fields%
end
set %DataFile% %Return:Value%

# Now extract each row
foreach %Row% in (response %Rows%)
    set %Record% withvector
    end
    foreach %Cell% in (selectIn %Row% ./td)
        putin %Record% (trim %Cell%)
    end

    call unit.file.WriteDataFile with
        %DataFile% %DataFile%
        %Record% %Record%
    end
end

call unit.file.CloseDataFile with
    %DataFile% %DataFile%
end

我的CSV文件如下所示。请注意,维基百科在每个单元格中都有提取信息。有很多方法可以摆脱它,但不是通用的方式。

Shot put,22.47 m,"Timmermann, UlfUlf Timmermann",East Germany (GDR),1988 1988 Seoul,"01988-09-23 September 23, 1988",[25]
Discus throw,69.89 m,"Alekna, VirgilijusVirgilijus Alekna",Lithuania (LTU),2004 2004 Athens,"02004-08-23 August 23, 2004",[26]
Hammer throw,84.80 m,"Litvinov, SergeySergey Litvinov",Soviet Union (URS),1988 1988 Seoul,"01988-09-26 September 26, 1988",[27]
Javelin throw,90.57 m,"Thorkildsen, AndreasAndreas Thorkildsen",Norway (NOR),2008 2008 Beijing,"02008-08-23 August 23, 2008",[28]