用rvest包來抓取Google學術搜索數據

在這篇文章,主要展現的是如何抓取Google學術網頁。示例展現的是用rvest包來抓取做者博士指導老師的我的學術數據。咱們能夠看到他的合著者,論文被引用了多少次以及它們的附屬機構。Hadley Wickham在RStudio Blog中寫道:「rvest的靈感來源於相似beautiful soup這樣能夠輕易的從HTML網頁抓取數據的一些庫」。由於它被設計成跟magrittr一塊兒使用。咱們能夠經過一些簡單和易於理解的代碼塊組成的管道操做來表示複雜的操做。css

加載R包: html

ggplot2包來做圖node

library(rvest)
library(ggplot2)

他的論文被引用了多少次?

使用SelectorGadget的CSS選擇器來找出"cited by"列。web

page <- read_html("https://scholar.google.com/citations?user=sTR9SIQAAAAJ&hl=en&oi=ao")

html_nodes()中指定CSS選擇器,html_text()用來提取文本。最後,用as.numernic()將字符串類型轉換爲數值類型。api

citations <- page %>% html_nodes ("#gsc_a_b .gsc_a_c") %>% html_text()%>%as.numeric()

查看引用這次數:ide

citations 
148 96 79 64 57 57 57 55 52 50 48 37 34 33 30 28 26 25 23 22

繪製引用次數的條形圖:ui

barplot(citations, main="How many times has each paper been cited?", ylab='Number of citations', col="skyblue", xlab="")


合著者,他們的附屬單位以及被引用的次數

一樣,咱們使用SelecotGadget的CSS選擇器來找出匹配的合著者:google

page <- read_html("https://scholar.google.com/citations?view_op=list_colleagues&hl=en&user=sTR9SIQAAAAJ")
Coauthors = page%>% html_nodes(css=".gsc_1usr_name a") %>% html_text()
Coauthors = as.data.frame(Coauthors)
names(Coauthors)='Coauthors'

查看下合著者spa

head(Coauthors) 
                  Coauthors
1               Jason Evans
2             Mutlu Ozdogan
3            Rasmus Houborg
4          M. Tugrul Yilmaz
5 Joseph A. Santanello, Jr.
6              Seth Guikema

dim(Coauthors) 
[1] 27  1

截止到2016年1月1日,他的合著者共有27人。翻譯

他的合著者被引用了多少次?

page <- read_html("https://scholar.google.com/citations?view_op=list_colleagues&hl=en&user=sTR9SIQAAAAJ")
citations = page%>% html_nodes(css = ".gsc_1usr_cby")%>%html_text()

citations 
 [1] "Cited by 2231"  "Cited by 1273"  "Cited by 816"   "Cited by 395"   "Cited by 652"   "Cited by 1531" 
 [7] "Cited by 674"   "Cited by 467"   "Cited by 7967"  "Cited by 3968"  "Cited by 2603"  "Cited by 3468" 
[13] "Cited by 3175"  "Cited by 121"   "Cited by 32"    "Cited by 469"   "Cited by 50"    "Cited by 11"   
[19] "Cited by 1187"  "Cited by 1450"  "Cited by 12407" "Cited by 1939"  "Cited by 9"     "Cited by 706"  
[25] "Cited by 336"   "Cited by 186"   "Cited by 192"

經過全局替代提取數值字符串

citations = gsub('Cited by','', citations)

citations
 [1] " 2231"  " 1273"  " 816"   " 395"   " 652"   " 1531"  " 674"   " 467"   " 7967"  " 3968"  " 2603"  " 3468"  " 3175" 
[14] " 121"   " 32"    " 469"   " 50"    " 11"    " 1187"  " 1450"  " 12407" " 1939"  " 9"     " 706"   " 336"   " 186"  
[27] " 192"

將字符串轉成數值型,再獲得ggplot2可用的數據框格式:

citations = as.numeric(citations)
citations = as.data.frame(citations)

合著者的附屬機構

page <- read_html("https://scholar.google.com/citations?view_op=list_colleagues&hl=en&user=sTR9SIQAAAAJ")
affilation = page %>% html_nodes(css = ".gsc_1usr_aff")%>%html_text()
affilation = as.data.frame(affilation)
names(affilation)='Affilation'

建立一個由coauthors,citations和affiliation組成的數據框

cauthors=cbind(Coauthors, citations, affilation)

cauthors 
                             Coauthors citations                                                                                  Affilation
1                          Jason Evans      2231                                                               University of New South Wales
2                        Mutlu Ozdogan      1273    Assistant Professor of Environmental Science and Forest Ecology, University of Wisconsin
3                       Rasmus Houborg       816                    Research Scientist at King Abdullah University of Science and Technology
4                     M. Tugrul Yilmaz       395 Assistant Professor, Civil Engineering Department, Middle East Technical University, Turkey
5            Joseph A. Santanello, Jr.       652                                                  NASA-GSFC Hydrological Sciences Laboratory
.....

根據引用次數,對合著者從新排序

根據引用次數對合著者從新排序,以便獲得遞減的順序圖:

cauthors$Coauthors <- factor(cauthors$Coauthors, levels = cauthors$Coauthors[order(cauthors$citations, decreasing=F)])

ggplot(cauthors,aes(Coauthors,citations))+geom_bar(stat="identity", fill="#ff8c1a",size=5)+
theme(axis.title.y   = element_blank())+ylab("# of citations")+
theme(plot.title=element_text(size = 18,colour="blue"), axis.text.y = element_text(colour="grey20",size=12))+
              ggtitle('Citations of his coauthors')+coord_flip()

與他合著的科學家中,有引用超過了12000次。他的學生中像我(圖中最後一個)這樣的剛處在"學走路的階段"。

總結

在這篇文章,咱們看到了如何抓取Google學術數據。我抓取了我導師的帳戶,得到了論文引用次數數據,合著者的附屬機構以及他們被引用的次數。

正如咱們在這篇文章所看到的同樣,利用rvest包能夠很容易的抓取HTML網頁數據。一樣重要的是,SelectorGadget經過CSS選擇器能夠幫助咱們找出感興趣的數據。

修正:個人導師告訴我Google學術只收錄了他的小部分合著者。跟他合做發表的一些科學家以及一些引用不少次文章並無顯示出來。進一步,上面獲得的結果對於有些人來講是不符合常理的(如:資歷更深的人發表了更多的文章卻比資歷淺的人引用的次數更少)。所以,Google學術數據應該謹慎使用。

本文由雪晴數據網負責翻譯整理,原文請參考Google scholar scraping with rvest package做者Fisseha Berhane。轉載請註明原文連接http://www.xueqing.cc/cms/article/109

相關文章
相關標籤/搜索