如何使用R中的WikipediR软件包从Wikipedia页面获取数据?
问题描述:
我需要从多个维基百科页面中获取某部分数据。我如何使用WikipediR软件包来做到这一点?或者还有其他更好的选择。准确地说,我只需要所有页面中的以下标记部分。如何使用R中的WikipediR软件包从Wikipedia页面获取数据?
我怎样才能得到呢?任何帮助,将不胜感激。
答
你可以更具体一点,你想要什么?以下是从网络导入数据的简单方法,特别是从Wikipedia导入数据。现在
library(rvest)
scotusURL <- "https://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States"
## ********************
## Option 1: Grab the tables from the page and use the html_table function to extract the tables you're interested in.
temp <- scotusURL %>%
html %>%
html_nodes("table")
html_table(temp[1]) ## Just the "legend" table
html_table(temp[2]) ## THE MAIN TABLE
,如果你想从具有结构基本相同的多个页面导入数据,但也许只是一些数字或一些改变,请尝试此方法。
library(RCurl);library(XML)
pageNum <- seq(1:10)
url <- paste0("http://www.totaljobs.com/JobSearch/Results.aspx?Keywords=Leadership<xt=&Radius=10&RateType=0&JobType1=CompanyType=&PageNum=")
urls <- paste0(url, pageNum)
allPages <- lapply(urls, function(x) getURLContent(x)[[1]])
xmlDocs <- lapply(allPages, function(x) XML::htmlParse(x))
到目前为止,您有尝试过什么吗? – MichaelChirico
你想要什么格式?我想表格的HTML代码不会有帮助... – Tgr
@tgr对于我来说,任何格式的数据都可以。我可以获取表格的HTML代码,但必须仔细清理数据。 –