我有解决方案想从网址抓取一些内容。
我在server.js中有代码:
let request = require('request');
let cheerio = require('cheerio');
let url = "domain[dot]com/title-to-video";
request(url, function(error, response, html){
if (!error && response.statusCode == 200) {
let $ = cheerio.load(html);
console.log($.text());
} else {
console("We've encountered an error: " + error);
}
});
它返回html:
<html>
<head>
<title>Website Title</title>
</head>
<body>
...
<script>
getplayer.setvd1('http://abc[dot]com/video34345453.mp4');
getplayer.setvd2('http://abc[dot]com/video43243234.mp4');
</script>
...
</body>
</html>
我希望输出为:
{
http://abc[dot]com/video34345453.mp4,
http://abc[dot]com/video43243234.mp4
}
更新: 在互联网上找到后,我试着修复:
const strvd1 = $('script:not([src])')[0].children[0].data;
const resultvd1 = strvd1.match(/setvd1\('(https:.+?)'\);/)[1];
output: http://abc[dot]com/video34345453.mp4
const strvd2 = $('script:not([src])')[0].children[0].data;
const resultvd2 = strvd2.match(/setvd2\('(https:.+?)'\);/)[1];
output: http://abc[dot]com/video43243234.mp4
感谢任何解决方案。
答案 0 :(得分:2)
在if语句中,尝试类似:
var $ = cheerio.load(html),
script = $('script').text(),
scriptParts = script.split(';');
console.log('{');
for (var i = 0; i < scriptParts.length; i++) {
if (scriptParts[i].trim().length > 0) {
var startPosition = scriptParts[i].indexOf('(') + 2,
endPosition = scriptParts[i].indexOf(')') - 1;
console.log(scriptParts[i].slice(startPosition, endPosition) + ',');
}
}
console.log('}');
答案 1 :(得分:1)
您可以按如下方式访问脚本内容:
1) $(&#39; script&#39;)。get()[0] .attribs [//属性名称]
2) let $ = cheerio.load(html,{xmlMode:false});