Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web scraping 如何使用rvest进行刮伤?_Web Scraping_Rvest - Fatal编程技术网

Web scraping 如何使用rvest进行刮伤?

Web scraping 如何使用rvest进行刮伤?,web-scraping,rvest,Web Scraping,Rvest,我需要从本页获得三个不同的数字(黄色,见图): 我使用rvest和inspectorgadget使用此代码: site=read_html("https://www.scopus.com/authid/detail.uri?authorId=7006040753") hindex=site %>% html_node(".row3 .valueColumn span")%>% html_text() documents=site %>% html_node("#docCn

我需要从本页获得三个不同的数字(黄色,见图):

我使用
rvest
inspectorgadget
使用此代码:

site=read_html("https://www.scopus.com/authid/detail.uri?authorId=7006040753") 
hindex=site %>% html_node(".row3 .valueColumn span")%>% html_text()
documents=site %>% html_node("#docCntLnk")%>% html_text()
citations=site %>% html_node("#totalCiteCount")%>% html_text()
print(citations)
我可以获得
h-index
文档
,但引用不起作用


你能帮我吗?

现在我找到了一个解决方案-我注意到加载值需要一些时间,所以我在PhnatomJS脚本中包含了一点超时。现在它在我的机器上工作,代码如下:

setwd("path/to/phantomjs/bin")
system('phantomjs readexample.js') # call PhantomJS script (stored in phantomjs/bin)

totalCiteCount <- "rendered_page.html" %>% # "rendered_page.html" is created by PhantomJS
   read_html() %>%
   html_nodes("#totalCiteCount") %>%
   html_text()

## totalCiteCount
## [1] "52018"
代码在R中抛出以下错误,但至少正确地刮取了值

> system('phantomjs readexample.js') TypeError: undefined is not a constructor (evaluating 'mutation.addedNodes.forEach')

  https://www.scopus.com/gzip_N1846232499/bundles/SiteCatalystTop.js:73  :0 in forEach   https://www.scopus.com/gzip_N1846232499/bundles/SiteCatalystTop.js:73 ReferenceError: Can't find variable: SDM

  https://www.scopus.com/gzip_N1729184664/bundles/AuthorProfileTop.js:73 in sendIndex   https://www.scopus.com/gzip_N1729184664/bundles/AuthorProfileTop.js:67 in loadEvents
使用PhantomJS非常方便,因为您不必安装任何东西(如果您的计算机上没有管理员权限,也可以使用PhantomJS)。只需将.zip文件解压缩到任意文件夹即可。然后将R(
setwd()
)中的工作目录设置为“phantomjs/bin”文件夹,它就可以工作了

您还可以在R中更改PhantomJS脚本(如果需要,可以迭代),例如在循环中向脚本传递不同的URL。例如:

for (i in 1:n_urls) {

   url_var <- urls[i] # assuming you have created a var "urls" with multiple URLs before
   lines <- readLines("readexample.js")
   lines[2] <- paste0("var url ='", url_var ,"';") # exchange code line with new URL
   writeLines(lines, "readexample.js") # new url is stored in the PhantomJS script

   system('phantomjs readexample.js')

   # <any code> #

} 
for(i在1:n\u URL中){

url_var乍一看像是一个快速修复程序,但是这个值似乎是动态加载的(如果您查看源代码,您会注意到数字没有出现在任何地方)这意味着您必须首先使用PhantomJS或RSelenium呈现网站,然后使用rvestI下载/处理网站。我刚刚尝试了PhantomJS,并且在这里遇到问题,因为页面处理似乎被阻止。因此,请使用RSelenium(不幸的是,我不知道这一点)或者用文献的数量而不是总引文的数量来计算,这样可以更容易地获得(这对我来说更有意义,因为它更重要的是引用了多少文献,而不是每一篇文献引用多少次)你好,汤姆,谢谢你的评论……你这是什么意思“如果你看源代码,你会注意到数字没有出现在任何地方”。实际上,在html源代码中,我找到了
,还有
51971
。很抱歉,我有点不准确。如果你使用右键单击->显示页面源代码(而不是浏览器的“检查元素”功能)它看起来是这样的:
由于一些错误,这也是使用PhantomJS打开和下载页面时得到的=>页面未呈现。此外,如果您刮取父节点
div class=“valueColumn”数据citedbythreshold=“false“>
您得到了除所需值之外的整行Hi Guglielmo,相关问题:您有没有找到一种方法来获得所有现有Scopus作者ID的列表?我注意到他们使用了多达11个看似随机的数字,所以只需将它们全部删除(990亿页)似乎效率很低/不可能,但我在任何地方都找不到完整的列表..谢谢!很棒的TomS!我会尝试你的解决方案…谢谢你。超级!!!!尝试了迭代…效果非常好!非常感谢Tom!!
for (i in 1:n_urls) {

   url_var <- urls[i] # assuming you have created a var "urls" with multiple URLs before
   lines <- readLines("readexample.js")
   lines[2] <- paste0("var url ='", url_var ,"';") # exchange code line with new URL
   writeLines(lines, "readexample.js") # new url is stored in the PhantomJS script

   system('phantomjs readexample.js')

   # <any code> #

}