如何从R访问维基百科?

mja*_*iec 8 wikipedia r text-mining wikipedia-api mediawiki-api

是否有任何R包允许查询维基百科(最有可能使用Mediawiki API)获取与此类查询相关的可用文章列表,以及导入文本挖掘的选定文章?

Ben*_*Ben 11

WikipediR一个'R中的MediaWiki API包装器'

library(devtools)
install_github("Ironholds/WikipediR")
library(WikipediR)
Run Code Online (Sandbox Code Playgroud)

它包括以下功能:

ls("package:WikipediR")
 [1] "wiki_catpages"      "wiki_con"           "wiki_diff"          "wiki_page"         
 [5] "wiki_pagecats"      "wiki_recentchanges" "wiki_revision"      "wiki_timestamp"    
 [9] "wiki_usercontribs"  "wiki_userinfo"  
Run Code Online (Sandbox Code Playgroud)

在这里使用它,获取一堆用户的贡献详细信息和用户详细信息:

library(RCurl)
library(XML)

# scrape page to get usernames of users with highest numbers of edits
top_editors_page <- "http://en.wikipedia.org/wiki/Wikipedia:List_of_Wikipedians_by_number_of_edits"
top_editors_table <- readHTMLTable(top_editors_page)
very_top_editors <- as.character(top_editors_table[[3]][1:5,]$User)

# setup connection to wikimedia project 
con <- wiki_con("en", project = c("wikipedia"))

# connect to API and get last 50 edits per user
user_data <- lapply(very_top_editors,  function(i) wiki_usercontribs(con, i) )
# and get information about the users (registration date, gender, editcount, etc)
user_info <- lapply(very_top_editors,  function(i) wiki_userinfo(con, i) )
Run Code Online (Sandbox Code Playgroud)


Ric*_*ton 7

使用RCurl包进行retreiving信息,使用XMLRJSONIO包解析响应.

如果您在代理后面,请设置选项.

opts <- list(
  proxy = "136.233.91.120", 
  proxyusername = "mydomain\\myusername", 
  proxypassword = 'whatever', 
  proxyport = 8080
)
Run Code Online (Sandbox Code Playgroud)

使用该getForm函数访问API.

search_example <- getForm(
  "http://en.wikipedia.org/w/api.php", 
  action  = "opensearch", 
  search  = "Te", 
  format  = "json",
  .opts   = opts
)
Run Code Online (Sandbox Code Playgroud)

解析结果.

fromJSON(rawToChar(search_example))
Run Code Online (Sandbox Code Playgroud)