2011-05-23 60 views
8

R是否有任何軟件包允許查詢維基百科(很可能使用Mediawiki API)以獲取與此類查詢相關的可用文章列表,以及爲文本挖掘導入所選文章?如何從R訪問Wikipedia?

+0

您可能會發現以下有用的內容:http://www.ragtag.info/2011/feb/10/processing-every-wikipedia-article/ – James 2011-05-23 10:57:27

回答

5

使用RCurl軟件包進行retreiving info,使用XMLRJSONIO軟件包來分析響應。

如果您位於代理之後,請設置您的選項。

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

使用getForm函數來訪問the API

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

解析結果。

fromJSON(rawToChar(search_example)) 
+0

我在使用此功能時遇到了一些搜索字詞的問題,但我懷疑它是與我在網絡上的問題。我需要志願者用'search'參數中的不同字符串來檢查示例代碼。 – 2011-05-23 13:43:19

9

WikipediR

library(devtools) 
install_github("Ironholds/WikipediR") 
library(WikipediR) 

'在R A鏈接到MediaWiki API包裝' 它包括以下功能:

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" 

這是在使用中,獲得的貢獻細節和用戶一堆用戶的詳細信息:

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))