【问题标题】:Use PhantomJS with pjscrape to scrape dynamically generated web content使用 PhantomJS 和 pjscrape 抓取动态生成的网页内容
【发布时间】:2015-11-03 19:39:59
【问题描述】:

我刚开始使用 pjscrape 并尝试运行http://nrabinowitz.github.io/pjscrape/#overview 上提供的示例爬虫,并在终端中调用了以下命令,但它给了我错误:

$ phantomjs /Applications/nrabinowitz-pjscrape-600e20a/pjscrape.js my_config.js

TypeError: undefined is not an object (评估 'phantom.args.length')

/Applications/nrabinowitz-pjscrape-600e20a/pjscrape.js:834 全局 代码^Z

[6]+ 停止 phantomjs /Applications/nrabinowitz-pjscrape-600e20a/pjscrape.js my_config.js

但后来我发现这可能是因为在 pjscrape.js 文件中,它使用了 phantom.args.length,在较新的 phantomJS 中已经被 system.args 替换。于是我修改了原来的pjscrape.js:

var system = require('system');
// make sure we have a config file
if (!system.args.length) {
// die
console.log('Usage: pjscrape.js <configfile.js> ...');
phantom.exit();
} else {

// load the config file(s)
system.args.forEach(function(configFile) {
    if (!phantom.injectJs(configFile)) {
        fail('Config file not found: ' + configFile);
     }
    });
 }

然后我运行相同的命令,但它给了我以下错误:

RangeError: 超出最大调用堆栈大小。

injectJs RangeError 中的未定义:0:最大调用堆栈大小 超过了。

injectJs 中的未定义:0
/Applications/nrabinowitz-pjscrape-600e20a/pjscrape.js:844 :0 在 forEach RangeError: 超出最大调用堆栈大小。

injectJs 中的未定义:0
/Applications/nrabinowitz-pjscrape-600e20a/pjscrape.js:844 :0 在 forEach RangeError: 超出最大调用堆栈大小。

/Applications/nrabinowitz-pjscrape-600e20a/pjscrape.js:850 全局 代码:0 injectJs
/Applications/nrabinowitz-pjscrape-600e20a/pjscrape.js:844 :0 在 forEach RangeError: 超出最大调用堆栈大小。

injectJs 中的未定义:0
/Applications/nrabinowitz-pjscrape-600e20a/pjscrape.js:844 :0 在 forEach 致命错误:未配置套件

现在我真的不知道出了什么问题。我的 my_config.js(我使用 pjscrape 定义刮板的地方)看起来像这样:

pjs.addSuite({
// url to scrape
url: 'http://en.wikipedia.org/wiki/List_of_towns_in_Vermont',
// selector to look for
scraper: '#sortable_table_id_0 tr td:nth-child(2)'
});

有人可以帮我弄清楚如何解决问题吗? 最终,我希望能够抓取一个网站,其中我需要的内容由 javascript 计算器生成,该计算器只接受一组输入并一次生成一个结果。但我也想在计算器中输入数千种不同的输入,并获得一个巨大的结果表。

【问题讨论】:

    标签: javascript web-scraping phantomjs dynamically-generated


    【解决方案1】:

    system.args 返回所有 命令行参数,包括 原始脚​​本,而phantom.args 只返回脚本文件之后的命令行参数。基本上,它进入了一个无限循环,因为它不断注入pjscrape.js 文件,直到达到最大调用堆栈大小。

    解决方案:拼接掉第一个参数:system.args.splice(1).forEach...

    【讨论】:

    • 你这个疯子!这成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2013-07-10
    • 2023-01-29
    • 2015-07-04
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多