cheerio 获取script标签中的数组
<script> var $ = document.getElementById; var data = []; data[“A1”] = “9”; data[“A10”] = “9.5”; data[“A101”] = “19.5”; data[“A11”] = “9.5”; data[“A12”] = “10.5”; data[“A13”] = “11”; </script>
有一段这种代码,我想用cheerio获取里边data数组的数据。看了一遍API好像没有合适的。有什么方法可以获得到这个数据吗?
6 回复
f12321321 From 变异后的 CNode+
这种情况,直接正则搞起。
'<script> var $ = document.getElementById; var data = []; data["A1"] = "9"; data["A10"] = "9.5"; data["A101"] = "19.5"; data["A11"] = "9.5"; data["A12"] = "10.5"; data["A13"] = "11"; </script>'
.match(/"[0-9,.]+"/g);
[""9"", ""9.5"", ""19.5"", ""9.5"", ""10.5"", ""11""]
不知道用eval可以么? 把整段标签的内容先拿到,然后加一句输出data,再 eval下,就能拿到data了
let str = `
var data = [];
data["A1"] = "9";
data["A10"] = "9.5";
data["A101"] = "19.5";
data["A11"] = "9.5";
data["A12"] = "10.5";
data["A13"] = "11";
`;
let what = eval(str + "data;");
console.log(what);
Eval is evil.
可以用 vm.runInContext