如何使用 R 中的 WikipediR 包从维基百科页面获取数据? [英] How to get data from Wikipedia page using WikipediR package in R?

查看:12
本文介绍了如何使用 R 中的 WikipediR 包从维基百科页面获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从多个维基百科页面中获取特定部分的数据.我如何使用 WikipediR 包来做到这一点?或者还有其他更好的选择.准确地说,我只需要所有页面中的以下标记部分.

I need to fetch a certain part of data from multiple Wikipedia pages. How can I do that using WikipediR package? Or is there some other better option for the same. To be precise, I need only the below marked part from all the pages.

我怎样才能得到它?任何帮助,将不胜感激.

How can I get that? Any help would be appreciated.

推荐答案

你能说得更具体一点吗?这是一种从网络(尤其是维基百科)导入数据的简单方法.

Can you be a little more specific as to what you want? Here's a simple way to import data from the web, and specifically from 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

现在,如果您想从具有基本相同结构的多个页面导入数据,但可能只是更改了一些数字或其他内容,请尝试这种方法.

Now, if you want to import data from multiple pages that have essentially the same structure, but maybe just change by some number or something, please try this method.

library(RCurl);library(XML)

pageNum <- seq(1:10)
url <- paste0("http://www.totaljobs.com/JobSearch/Results.aspx?Keywords=Leadership&LTxt=&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))

这篇关于如何使用 R 中的 WikipediR 包从维基百科页面获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆