在R中导入维基百科表

时间:2011-09-13 20:00:58

标签: r dataframe

我经常从维基百科中提取表格。 Excel的Web导入对维基百科无效,因为它将整个页面视为表格。在谷歌电子表格中,我可以输入:

=ImportHtml("http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan","table",3)

此功能将从该页面下载第3张表,其中列出了密歇根州UP的所有县。

R中有类似的东西吗?或者可以通过用户定义的函数创建吗?

6 个答案:

答案 0 :(得分:12)

readHTMLTable中的函数XML非常适用于此。

尝试以下方法:

library(XML)
doc <- readHTMLTable(
         doc="http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan")

doc[[6]]

            V1         V2                 V3                              V4
1       County Population Land Area (sq mi) Population Density (per sq mi)
2        Alger      9,862                918                            10.7
3       Baraga      8,735                904                             9.7
4     Chippewa     38,413               1561                            24.7
5        Delta     38,520               1170                            32.9
6    Dickinson     27,427                766                            35.8
7      Gogebic     17,370               1102                            15.8
8     Houghton     36,016               1012                            35.6
9         Iron     13,138               1166                            11.3
10    Keweenaw      2,301                541                             4.3
11        Luce      7,024                903                             7.8
12    Mackinac     11,943               1022                            11.7
13   Marquette     64,634               1821                            35.5
14   Menominee     25,109               1043                            24.3
15   Ontonagon      7,818               1312                             6.0
16 Schoolcraft      8,903               1178                             7.6
17       TOTAL    317,258             16,420                            19.3

readHTMLTable为HTML页面的每个元素返回data.frame的列表。您可以使用names获取有关每个元素的信息:

> names(doc)
 [1] "NULL"                                                                               
 [2] "toc"                                                                                
 [3] "Election results of the 2008 Presidential Election by County in the Upper Peninsula"
 [4] "NULL"                                                                               
 [5] "Cities and Villages of the Upper Peninsula"                                         
 [6] "Upper Peninsula Land Area and Population Density by County"                         
 [7] "19th Century Population by Census Year of the Upper Peninsula by County"            
 [8] "20th & 21st Centuries Population by Census Year of the Upper Peninsula by County"   
 [9] "NULL"                                                                               
[10] "NULL"                                                                               
[11] "NULL"                                                                               
[12] "NULL"                                                                               
[13] "NULL"                                                                               
[14] "NULL"                                                                               
[15] "NULL"                                                                               
[16] "NULL" 

答案 1 :(得分:7)

这是一个与安全(https)链接一起使用的解决方案:

install.packages("htmltab")
library(htmltab)
htmltab("http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan",3)

答案 2 :(得分:3)

以Andrie的答案为基础,并解决SSL问题。如果您可以采用一个额外的库依赖项:

{
    method: "GET", path: "/downloadFile",
    config: {auth: false},
    handler: function (request, reply) {
        // TODO
        reply({})
    }
},

答案 3 :(得分:2)

一种简单的方法是使用RGoogleDocs界面让Google文档为您进行转换:

http://www.omegahat.org/RGoogleDocs/run.html

然后,您可以将=ImportHtml Google文档功能与其预先制作的所有魔法一起使用。

答案 4 :(得分:0)

使用 tidyverservest 解决方案。如果您需要根据某些关键字(例如在表格标题中)查找表格,这将非常有用。下面是一个示例,我们希望获取有关埃及人口统计数据的表格。注意:html_nodes(x = page, css = "table") 是浏览页面上可用表格的有用方式。

library(magrittr)
library(rvest)

# define the page to load
read_html("https://en.wikipedia.org/wiki/Demographics_of_Egypt") %>% 
    # list all tables on the page
    html_nodes(css = "table") %>% 
    # select the one containing needed key words
    extract2(., str_which(string = . , pattern = "Live births")) %>% 
    # convert to a table
    html_table(fill = T) %>%  
    view

答案 5 :(得分:0)

该表是唯一的表,它是第二个 td 子项的子项,因此您可以使用 css 指定该模式。您可以使用更快的类,而不是使用 table 的类型选择器来获取子表:

library(rvest)

t <- read_html('https://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan') %>% 
  html_node('td:nth-child(2) .wikitable') %>% 
  html_table()

print(t)