简体   繁体   English

为什么用cheerio运行这段代码没有output?

[英]Why is there no output when I run this code with cheerio?

I am trying to get all the gif links from https://giphy.com/search/test and put them into an array using request and cheerio.我正在尝试从https://giphy.com/search/test获取所有 gif 链接,并使用 request 和 cheerio 将它们放入数组中。 When I run my code nothing happens.当我运行我的代码时,什么也没有发生。 Does anyone know what I am doing wrong?有谁知道我做错了什么?

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

urls = [

];

    request("https://giphy.com/search/test", function(err, resp, body){
    if(!err && resp.statusCode == 200){
        var $ = cheerio.load(body);
        $("a.giphy-gif css-d0ovzf", "#GridWithTV-sc-1hnvpyn eLOeIu").each(function(){
            var url = this.attr('href');
            urls.push(url)
            console.log(urls)
        })

    }
});

Converting an excellent comment to an answer, the data you get from an HTTP request is just the plain HTML. This website is a single page app which is mostly JS-driven.优秀评论转换为答案,您从 HTTP 请求中获得的数据只是普通的 HTML。该网站是一个单页应用程序,主要由 JS 驱动。 The selectors are built after the page loads and the browser executes JavaScript.选择器在页面加载后构建,浏览器执行 JavaScript。

However, most of the data that goes onto the page is available in a JSON blob in the static HTML and can be extracted with a regex:但是,进入页面的大部分数据都在 JSON blob 中可用,位于 static HTML 中,并且可以使用正则表达式提取:

const cheerio = require("cheerio");
require("util").inspect.defaultOptions.depth = null;

const url = "<Your URL>";

fetch(url)
  .then(res => {
    if (!res.ok) {
      throw Error(res.statusText);
    }

    return res.text();
  })
  .then(html => {
    const match = html.match(/^ *gifs: (.*),$/m);
    console.log(JSON.parse(match[1]));
  })
  .catch(err => console.error(err));

From here, it's a matter of grabbing the data you want, for example:从这里开始,就是获取所需数据的问题了,例如:

const data = JSON.parse(match[1])
  .map(({title, url}) => ({title, url}));
console.log(data);

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

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