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

时间:2018-10-07 02:34:37

标签: javascript r phantomjs

我需要从此页面获取包含一些脚本的内容: 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。 对于其他包含js的页面,它可以正常工作,但不适用于我需要的页面。

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

system(“ 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){返回{key:t.version,text:t [“ name _” + e.root.navigation.lang],value:t.version }}大于30

https://grouper.swissdrg.org/packs/App-3dd15966701d9f6fd4db.js:1 in br 未处理的承诺拒绝TypeError:未定义的不是构造函数(正在评估'n.push(this.pdx)')

1 个答案:

答案 0 :(得分:1)

您可能需要更长的超时时间。我必须使用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")

}

现在,我们将使用超时时间更长的URL:

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

请注意,phantomjs被认为是旧版工具,因为自从无头Chrome出现以来,主要开发人员已将其转移。不幸的是,无法在简单的cmd界面中为无头Chrome设置超时,因此您暂时还无法使用phantomjs。

我建议尝试splashr,但您使用的是Windows,并且splashr需要Docker;或者,decapitated有一个业务流程副本gepetto,但这需要nodejs;这些组合中的任何一个似乎都可能使人们无法使用该旧版操作系统。