首页 > 解决方案 > 如何使用 Cheerio 获取 javascript 变量

问题描述

当我在 Postman 中检查请求数据时,它返回 HTML 文件并包含 JavaScript 值。

<html>
  <body>
     HTML VALUES...
  </body>
  <script>
    var _ITEMID = '29041549';
    var ITEM_VID = '612c86e6c6f7840001d0c821';
    var _SOLDOUT = '1' == 0;
    var _DISCOUNTPRICE = '110.00';
    var _SIZE = 'S,M,L,XL';
    var _ITEMTYPE = '0';
    var _COLORPICS = '';
  </script>
  <script>another script value</script>
  <script>another script value</script>
  <script>another script value</script>
  <script>another script value</script>
</html>

我使用got包来请求数据。我想获取脚本的值,但是当我尝试这段代码时,它返回未定义。

我已经检查了这篇文章并修改了这些代码,但它似乎不适合我的情况。

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

const data = await got(`https://www.vvic.com/item/${itemDetailURL}.html`);
// console.log(data);
const $ = cheerio.load(data);
const calData = $('script').get()[0];
console.log(calData);  // Returns undefined

[console.log($('script'));]

LoadedCheerio {
  length: 0,
  options: { xml: false, decodeEntities: true },
  _root: <ref *1> LoadedCheerio {
    '0': Document {
      type: 'root',
      parent: null,
      prev: null,
      next: null,
      startIndex: null,
      endIndex: null,
      children: [Array]
    },
    length: 1,
    options: { xml: false, decodeEntities: true },
    _root: [Circular *1]
  },
  prevObject: <ref *1> LoadedCheerio {
    '0': Document {
      type: 'root',
      parent: null,
      prev: null,
      next: null,
      startIndex: null,
      endIndex: null,
      children: [Array]
    },
    length: 1,
    options: { xml: false, decodeEntities: true },
    _root: [Circular *1]
  }
}

[console.log($('script').get());]

[] // Returns empty array

标签: javascriptnode.jscheerio

解决方案


推荐阅读