简体   繁体   English

将参数传递给生成器,vo

[英]passing argument to generators, vo

I'm trying to scrape an entire page using nightmare js and return the result to the calling function. 我正在尝试使用噩梦js抓取整个页面,并将结果返回给调用函数。 In order to do so, by extending the nightmare example, I pass an argument to a generator function. 为此,通过扩展噩梦示例,我将一个参数传递给生成器函数。 For a reason I don't understand, the run() function never gets called. 由于我不明白的原因,永远不会调用run()函数。

Thanks for your help. 谢谢你的帮助。

var Nightmare = require('nightmare');
var vo = require('vo');
const fs = require('fs');

url = "http://google.com";

vo( run(url) )(function(err, result) {
    if (err) throw err;
    console.log("end result length: ", result.length);

    fs.writeFile("test.html", result, function(err) {
        if(err) { return console.log(err); } })

 });

function *run(url) {
    console.trace()
    var x = Date.now();
    var nightmare = Nightmare();
    var html = yield nightmare
      .goto(url) // 'http://google.com')
      .evaluate(function() {
        return document.getElementsByTagName('html')[0].innerHTML;
      });

   console.log("done in " + (Date.now()-x) + "ms");
    console.log("result:", html.length);

    yield nightmare.end();
    return html;
}

It's just wrong terminology in the example above. 在上面的示例中,这只是错误的术语。 This is how vo works: vo的工作方式如下:

vo( run )(url1, function(err, result) {
    if (err) throw err;
    console.log("end result length: ", result.length);

    fs.writeFile("test.html", result, function(err) {
        if(err) { return console.log(err); } })

 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM