Skip to content

Instantly share code, notes, and snippets.

@masaha03
Created May 15, 2011 11:41
Show Gist options
  • Save masaha03/973078 to your computer and use it in GitHub Desktop.
Save masaha03/973078 to your computer and use it in GitHub Desktop.
KAKEN APIテスト
# 科研検索APIのヘルプ http://kaken.nii.ac.jp/ja/help/#opensearch-form
# 課題検索OpenSearchテスト
test1 <- readLines("http://kaken.nii.ac.jp/opensearchk.cgi?q15=60273570", encoding="UTF-8")
cat(test1, fill=TRUE)
# 研究者検索OpenSearchテスト
test2 <- readLines('http://kaken.nii.ac.jp/opensearchr.cgi?q1="社会学"', encoding="UTF-8")
cat(test2, fill=TRUE)
# 課題RDF取得テスト
test3 <- readLines("http://kaken.nii.ac.jp/rdf/p/19330106", encoding="UTF-8")
cat(test3, fill=TRUE)
# 研究者番号から課題検索し,研究者のネットワークを抽出
# 課題検索→各課題のRDFを取得→RDFから研究代表者・研究分担者の研究者番号を取得
library(XML)
search.result <- xmlParse(enc2utf8("http://kaken.nii.ac.jp/opensearchk.cgi?q15=60273570")) # 課題検索の結果を取得(URLに日本語が含まれる場合はUTF8に変換する必要がある)
search.result <- xmlToList(search.result) # XMLをlistに変換
search.result <- rapply(search.result, function(x){iconv(x,"UTF-8","CP932")}, how="replace") # 文字コード変換
entries <- search.result[which(names(search.result)=="entry")] # search.result$entryを取得(要素名の重複への対処)
links <- sapply(entries, function(x){x$link}) # entries$entry$linkを取得
links <- sub("ja","rdf", links) # 課題ページのURLを課題RDFのURLに変換
membersNum <- list()
for (i in seq_along(links)){
kaken.page <- xmlParse(links[i]) # 課題PDF取得
kaken.page <- xmlToList(kaken.page)
kaken.page <- rapply(kaken.page, function(x){iconv(x,"UTF-8","CP932")}, how="replace")
project <- kaken.page$Project
members <- project[which(names(project)=="member")]
membersNum[[i]] <- sapply(members, function(x){x$Researcher$researcherNumber}) # members$Researcher$researcherNumberを取得
}
researcher.network <- unique(unlist(membersNum)) # 結合
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment