从维基百科加载 table 到 R

Load a table from wikipedia into R

我正在尝试从以下 URL 将最高法院大法官的 table 加载到 R 中。 https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States

我正在使用以下代码:

scotusURL <- "https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States"
scotusData <- getURL(scotusURL, ssl.verifypeer = FALSE)
scotusDoc <- htmlParse(scotusData)
scotusData <- scotusDoc['//table[@class="wikitable"]']
scotusTable <- readHTMLTable(scotusData[[1]], stringsAsFactors = FALSE)

R returns scotusTable 为 NULL。这里的目标是在 R 中获得一个 data.frame,我可以用它来制作 SCOTUS 在法庭上的司法任期的 ggplot。我以前用脚本来制作一个很棒的情节,但是在最近的决定之后页面上发生了一些变化,现在脚本将无法运行。我浏览了维基百科上的 HTML 以试图找到任何更改,但我不是网络开发人员,所以任何会破坏我的脚本的东西都不会立即显现出来。

此外,R 中是否有一种方法可以让我缓存此页面的数据,这样我就不会经常引用 URL?这似乎是将来避免此问题的理想方式。 感谢您的帮助。

顺便说一句,SCOTUS 在我正在进行的 hobby/side-project 中,所以如果有其他比维基百科更好的数据源,我会洗耳恭听。

编辑:抱歉,我应该列出我的依赖项。我正在使用 XML、plyr、RCurl、data.table 和 ggplot2 库。

如果您不介意使用不同的包,您可以尝试 "rvest" 包。

library(rvest)    
scotusURL <- "https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States"
  • 选项 1:从页面中抓取 table 并使用 html_table 函数提取您感兴趣的 table。

    temp <- scotusURL %>% 
      html %>%
      html_nodes("table")
    
    html_table(temp[1]) ## Just the "legend" table
    html_table(temp[2]) ## The table you're interested in
    
  • 选项 2:检查 table 元素并复制 XPath 以直接读取 table(右键单击,检查元素,滚动到相关的 "table" 标签,右键单击它,然后 select "Copy XPath").

    scotusURL %>% 
      html %>% 
      html_nodes(xpath = '//*[@id="mw-content-text"]/table[2]') %>% 
      html_table
    

我喜欢的另一个选择是在 Google 电子表格中加载数据并使用 "googlesheets" package.

读取它

在 Google 驱动器中,创建一个新的电子表格,例如命名为 "Supreme Court"。在第一个工作表中,输入:

=importhtml("https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States", "table", 2)

这会自动将此 table 抓取到您的 Google 电子表格中。

从那里开始,您可以在 R 中执行以下操作:

library(googlesheets)
SC <- gs_title("Supreme Court")
gs_read(SC)

你可以试试这个:

url <- "https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States"
library(rvest) #v 0.2.0.9000
the_table <- read_html(url) %>% html_node("table.wikitable:nth-child(11)") %>% html_table()

出于某种原因,googlesheets 依赖项无法正常工作,所以我还是通过 google 解决了它。

我运行:

=importhtml("https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States", "table", 2)

然后将文件下载为 .csv

不知道为什么我以前没有想到这一点。我将不得不重新编写我的字符串脚本来清理它,但这最终是 1) 解决我遇到的第一个问题和 2) 下载文件的最佳方法,这样我就不必一直引用 URL.

感谢您的帮助。

我会删除所有 <span style="display:none"> 节点并从 scotusDoc 读取 table 而不是尝试 select 已更改的 table class 值.

scotusDoc <- htmlParse(scotusData, encoding="UTF-8")
xpathSApply(scotusDoc, "//span[@style='display:none']", removeNodes)
x <- readHTMLTable(scotusDoc, which=2,stringsAsFactors=FALSE)

head(x)
  #           Judge State Born/Died         Active service Chief Justice Retirement Appointed by Reason for\ntermination
1 1       John Jay†    NY 1745–1829   1789–1795(5–6 years)     1789–1795          —   Washington             Resignation
2 2   John Rutledge    SC 1739–1800   1789–1791(1–2 years)             —          —   Washington        Resignation[n 1]
3 3 William Cushing    MA 1732–1810 1789–1810(20–21 years)             —          —   Washington                   Death
4 4    James Wilson    PA 1742–1798   1789–1798(8–9 years)             —          —   Washington                   Death
5 5 John Blair, Jr.    VA 1732–1800   1789–1795(5–6 years)             —          —   Washington             Resignation
6 6   James Iredell    NC 1751–1799   1790–1799(8–9 years)             —          —   Washington                   Death

这里是 table class,所以第二个 table 现在是 "wikitable sortable"

xpathSApply(scotusDoc, "//table", xmlGetAttr, "class")
[1] "wikitable"                                         "wikitable sortable"                               
[3] "navbox"                                            "nowraplinks collapsible autocollapse navbox-inner"
[5] "navbox"                                            "nowraplinks collapsible collapsed navbox-inner