用rvest包来抓取Google学术搜索数据
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
在這篇文章,主要展示的是如何抓取Google學(xué)術(shù)網(wǎng)頁(yè)。示例展示的是用rvest包來(lái)抓取作者博士指導(dǎo)老師的個(gè)人學(xué)術(shù)數(shù)據(jù)。我們可以看到他的合著者,論文被引用了多少次以及它們的附屬機(jī)構(gòu)。Hadley Wickham在RStudio Blog中寫道:“rvest的靈感來(lái)源于類似beautiful soup這樣可以輕易的從HTML網(wǎng)頁(yè)抓取數(shù)據(jù)的一些庫(kù)”。因?yàn)樗辉O(shè)計(jì)成跟magrittr一起使用。我們可以通過(guò)一些簡(jiǎn)單和易于理解的代碼塊組成的管道操作來(lái)表示復(fù)雜的操作。
加載R包:
用ggplot2包來(lái)作圖
library(rvest) library(ggplot2)他的論文被引用了多少次?
使用SelectorGadget的CSS選擇器來(lái)找出"cited by"列。
page <- read_html("https://scholar.google.com/citations?user=sTR9SIQAAAAJ&hl=en&oi=ao")在html_nodes()中指定CSS選擇器,html_text()用來(lái)提取文本。最后,用as.numernic()將字符串類型轉(zhuǎn)換為數(shù)值類型。
citations <- page %>% html_nodes ("#gsc_a_b .gsc_a_c") %>% html_text()%>%as.numeric()查看引用此次數(shù):
citations 148 96 79 64 57 57 57 55 52 50 48 37 34 33 30 28 26 25 23 22繪制引用次數(shù)的條形圖:
barplot(citations, main="How many times has each paper been cited?", ylab='Number of citations', col="skyblue", xlab="")合著者,他們的附屬單位以及被引用的次數(shù)
同樣,我們使用SelecotGadget的CSS選擇器來(lái)找出匹配的合著者:
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'查看下合著者
head(Coauthors) Coauthors 1 ? ? ? ? ? ? ? Jason Evans 2 ? ? ? ? ? ? Mutlu Ozdogan 3 ? ? ? ? ? ?Rasmus Houborg 4 ? ? ? ? ?M. Tugrul Yilmaz 5 Joseph A. Santanello, Jr. 6 ? ? ? ? ? ? ?Seth Guikemadim(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"通過(guò)全局替代提取數(shù)值字符串
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"將字符串轉(zhuǎn)成數(shù)值型,再得到ggplot2可用的數(shù)據(jù)框格式:
citations = as.numeric(citations) citations = as.data.frame(citations)合著者的附屬機(jī)構(gòu)
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'創(chuàng)建一個(gè)由coauthors,citations和affiliation組成的數(shù)據(jù)框
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 .....根據(jù)引用次數(shù),對(duì)合著者重新排序
根據(jù)引用次數(shù)對(duì)合著者重新排序,以便得到遞減的順序圖:
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()與他合著的科學(xué)家中,有引用超過(guò)了12000次。他的學(xué)生中像我(圖中最后一個(gè))這樣的剛處在"學(xué)走路的階段"。
總結(jié)
在這篇文章,我們看到了如何抓取Google學(xué)術(shù)數(shù)據(jù)。我抓取了我導(dǎo)師的賬戶,獲得了論文引用次數(shù)數(shù)據(jù),合著者的附屬機(jī)構(gòu)以及他們被引用的次數(shù)。
正如我們?cè)谶@篇文章所看到的一樣,利用rvest包可以很容易的抓取HTML網(wǎng)頁(yè)數(shù)據(jù)。同樣重要的是,SelectorGadget通過(guò)CSS選擇器可以幫助我們找出感興趣的數(shù)據(jù)。
修正:我的導(dǎo)師告訴我Google學(xué)術(shù)只收錄了他的小部分合著者。跟他合作發(fā)表的一些科學(xué)家以及一些引用很多次文章并沒(méi)有顯示出來(lái)。進(jìn)一步,上面得到的結(jié)果對(duì)于有些人來(lái)說(shuō)是不符合常理的(如:資歷更深的人發(fā)表了更多的文章卻比資歷淺的人引用的次數(shù)更少)。因此,Google學(xué)術(shù)數(shù)據(jù)應(yīng)該謹(jǐn)慎使用。
本文由雪晴數(shù)據(jù)網(wǎng)負(fù)責(zé)翻譯整理,原文請(qǐng)參考Google scholar scraping with rvest package作者Fisseha Berhane。轉(zhuǎn)載請(qǐng)注明原文鏈接http://www.xueqing.cc/cms/article/109
轉(zhuǎn)載于:https://my.oschina.net/u/2605101/blog/601281
總結(jié)
以上是生活随笔為你收集整理的用rvest包来抓取Google学术搜索数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基于 HanLP 的 ES 中文分词插件
- 下一篇: 如果正确看待区块链这一新型技术的未来发展