简体   繁体   English

无法将链接从一个功能传递到另一个功能

[英]Trouble passing links from one function to another

I've written a very small script in node.js using request and cheerio to parse titles connected to links which I've already scraped from it's landing page. 我在node.js使用requestcheerio编写了一个非常小的脚本,以分析与links titles ,这些links已经从它的登录页面中cheerio了。

The problem is I can't find any idea as to how I can pass links (populated from the first function) to the second function in order to get the titles from there. 问题是我找不到如何将链接(从第一个函数填充到第二个函数)以从那里获取标题的任何想法。 I'm very new to node.js so can't figure out how to return the result from the first function and pass them to the latter one. 我对node.js非常node.js因此无法弄清楚如何从第一个函数返回结果并将其传递给后一个函数。

The error I'm having: 我遇到的错误:

for (const link of links) {
                   ^
ReferenceError: links is not defined

This is the full script: 这是完整的脚本:

var request = require('request');
var cheerio = require('cheerio');

const url = 'https://news.ycombinator.com';

request(url, function (error, response, html) {
  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html);
    $('.title .storylink').each(function(){
    var links = $(this).attr("href");
    });
  }
});

for (const link of links) {
  (function(url) {
    request(url, function (error, response, html) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var title = $('title').eq(0).text();
        console.log(title);
      }
    });
})(link);
}

Btw, the selectors used within the script are flawless. 顺便说一句,脚本中使用的选择器是完美的。 All I need to know is pass the result from one function to another. 我所需要知道的是将结果从一个函数传递给另一个函数。

Your 'links' var is out of scope. 您的“链接”变量超出范围。 It is outside the callback in 'request'. 它在“请求”中的回调之外。 Either have to turn request into a promise, use request-promise, or move the for loop into the function to keep links in scope. 必须将请求变成承诺,使用请求承诺或将for循环移入函数以使链接保持在作用域内。

https://www.npmjs.com/package/request-promise https://www.npmjs.com/package/request-promise

https://scotch.io/courses/10-need-to-know-javascript-concepts/callbacks-promises-and-async https://scotch.io/courses/10-need-to-know-javascript-concepts/callbacks-promises-and-async

It seems I found myself a solution. 看来我找到了解决办法。 It did the trick: 它成功了:

var request = require('request');
var cheerio = require('cheerio');

const url = 'https://news.ycombinator.com';

function getItems(callback){
  request(url, function (error, response, html) {
    if (!error && response.statusCode == 200) {
      var $ = cheerio.load(html);
      $('.title .storylink').each(function(){
      var links = $(this).attr("href");
      return callback(links,false);
      });
    }
  });
}
getItems((link)=>{
    request(link, function (error, response, html) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var title = $('title').eq(0).text();
        console.log(title);
      }
    });
});

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

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