通过R中的phantomjs下载多个URL。如何遍历它们?

时间:2019-06-27 18:22:05

标签: r loops url web-scraping phantomjs

由于网站包含javascript,我正在尝试使用pahtomjs通过R下载多个URL。我可以使用下面最后一个代码框中的代码下载单个网页,但我需要它才能用于多个网站。为了找到该代码,我使用了本教程:http://flovv.github.io/Scrape-JS-Sites/  我有一个字符串urls,urls [i],一个用于保存urls [i]的目的地。 以前我尝试过:

for(i in seq_along(urls)) {
 download.file(urls[i], destinations[i], mode="wb")
}

但是,由于网站包含javascript,因此无法正常工作。

我试图遵循这篇文章中的答案,但感到非常困惑:Scraping multiple URLs by looping in PhantomJS

writeLines("var url = 'url link';
var page = new WebPage();
var fs = require('fs');

page.open(url, function (status) {
        just_wait();
});

function just_wait() {
    setTimeout(function() {
               fs.write('1.html', page.content, 'w');
            phantom.exit();
    }, 2500);
}
", con = "scrape.js")

js_scrape <- function(url = "url link", 
                      js_path = "scrape.js", 
                      phantompath = "phantomjs"){
  lines <- readLines(js_path)
  lines[1] <- paste0("var url ='", url ,"';")
  writeLines(lines, js_path)

  command = paste(phantompath, js_path, sep = " ")
  system(command)

}

js_scrape()

请帮助我!

0 个答案:

没有答案
相关问题