Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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
Javascript 使用小js脚本使用R和phantomjs进行Web抓取返回错误_Javascript_R_Phantomjs - Fatal编程技术网

Javascript 使用小js脚本使用R和phantomjs进行Web抓取返回错误

Javascript 使用小js脚本使用R和phantomjs进行Web抓取返回错误,javascript,r,phantomjs,Javascript,R,Phantomjs,我需要从包含一些脚本的页面获取内容: $$&provider=acute&locale=de。 对于其他包含js的页面,它可以正常工作,但对于我需要的页面则不行 phantomjs.exe位于根目录中,并通过系统调用(win7 64位)成功调用: 系统(“phantomjs WebScrapeV1.js”) java脚本文件WebScrapeV1.js如下所示: var url ='https://grouper.swissdrg.org/swissdrg/single?version=7.3&

我需要从包含一些脚本的页面获取内容: $$&provider=acute&locale=de。 对于其他包含js的页面,它可以正常工作,但对于我需要的页面则不行

phantomjs.exe位于根目录中,并通过系统调用(win7 64位)成功调用:

系统(“phantomjs WebScrapeV1.js”)

java脚本文件WebScrapeV1.js如下所示:

var url ='https://grouper.swissdrg.org/swissdrg/single?version=7.3&pc=1337_70_0_0_M_11_00_15_0_2018/08/07_2018/08/22_C18.4_C07_-_45.81.11$$&provider=acute&locale=de';
var page = new WebPage()
var fs = require('fs');
page.open(url, function (status) {
  just_wait();
});
function just_wait() {
  setTimeout(function() {
    fs.write('WebScrapeV1.html', page.content, 'w');
    phantom.exit();
  }, 2500);
}
这是我得到的错误:

错误:[mobx.array]索引超出范围,函数(t){return{key:t.version,text:t[“name_880;”+e.root.navigation.lang],值:t.version}大于30

在br中
未处理的承诺拒绝类型错误:undefined不是构造函数(评估'n.push(this.pdx)')

您可能需要更长的超时时间。我不得不使用3600来获取所有内容(那个网站对我来说太慢了)。这里有一种方法可以在发生错误时修改超时,而无需手动修改phantomjs脚本

首先,我们将创建一个函数来总结所有的复杂性:

#' Read contents from a URL with phantomjs
#' 
#' @param url the URL to scrape
#' @param timeout how long to wait, default is `2500` (ms)
#' @param .verbose, if `TRUE` (the default), display the generated 
#'        scraping script and any `stdout` output from phantomjs
read_phantom <- function(url, timeout=2500, .verbose = TRUE) {

  suppressPackageStartupMessages({
    require("glue", character.only = TRUE, quiet=TRUE)
    require("crayon", character.only = TRUE, quiet=TRUE)
  })

  phantom_template <- "
var url = {url};
var page = new WebPage()
var fs = require('fs');
page.open(url, function (status) {{
  just_wait();
});
function just_wait() {{
  setTimeout(function() {{
    fs.write({output_file}, page.content, 'w');
    phantom.exit();
  }, {timeout});
}
" 

  url <- shQuote(url)

  phantom_bin <- Sys.which("phantomjs")

  tf_in <- tempfile(fileext = ".js")
  on.exit(unlink(tf_in), add=TRUE)

  tf_out <- tempfile(fileext = ".html")
  on.exit(unlink(tf_out), add=TRUE)

  output_file <- shQuote(tf_out)

  phantom_script <- glue(phantom_template)

  if (.verbose) {
    cat(
      crayon::white("Using the following generated scraping script:\n"),
      crayon::green(phantom_script), "\n", sep=""
    )
  }

  writeLines(phantom_script, tf_in)

  system2(
    command = phantom_bin, 
    args = tf_in,
    stdout = if (.verbose) "" else NULL
  )

  paste0(readLines(tf_out, warn = FALSE), collapse="\n")

}
使用phantomjs从URL读取内容 #' #“@param url要刮取的url #'@param timeout等待多长时间,默认值为'2500'(毫秒) #'@param.verbose,如果'TRUE'(默认值),则显示生成的 #'抓取脚本和来自phantomjs的任何'stdout'输出
谢谢你的努力。我曾在不同的Win 7 PC上尝试过该功能。不幸的是,我仍然会遇到与上述相同的错误。可能您使用的是linux操作系统?我启动了Win 7虚拟机,果然phantomjs抛出了类似的错误,这可能意味着由于Windows的缺陷,它的编译有一些隐含的限制(V8软件包在Windows上用于js虚拟机的DLL也有类似的问题)。只是在windows上使用headless chrome进行了尝试,但由于无法指定更长的超时时间,它会截断那里的HTML输出。上面的解决方案似乎只适用于非Win操作系统。在这个糟糕的操作系统上,b/c phantomjs几乎是瘫痪的。
read_phantom(
  url = "https://grouper.swissdrg.org/swissdrg/single?version=7.3&pc=1337_70_0_0_M_11_00_15_0_2018/08/07_2018/08/22_C18.4_C07_-_45.81.11$$&provider=acute&locale=de",
  timeout = 3600
) -> doc

substr(doc, 1, 100)
## [1] "<html><head>\n<script src=\"https://js-agent.newrelic.com/nr-1071.min.js\"></script><script type=\" text"

nchar(doc)
## [1] 26858