用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

相关文章
相关标签/搜索