javascript - 如何使用 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
解决方案
推荐阅读
- r - 在 r 中使用 dplyr 根据条件减去日期
- wordpress - AWS EC2 apache 网络服务器根的权限
- c++ - Xerces C++ 方法将更新的 xml 写回字符串
- python - 如何并行运行机器人框架测试用例而不是并行测试套件?
- python - 在字典中搜索精确值
- c++ - Math::getGradeTotal(string continuegrade) 中的 while 循环给出错误
- javascript - 用css在滑块的中心插入图形
- sql - 使用 50 个并行 SQLPLUS 会话和 Bulk Collect LIMIT 和 FORALL 更新具有 500+ 百万条记录的表
- android - 如何更改输入反应本机的值
- python - Scrapy Mongodb 管道超时