有没有办法从带有cheerio的脚本标签中获取js脚本

问题描述

所以我正在尝试使用cheerio从我的脚本标签HTML中获取数据

这是我的 scripttag.html :

<html>
<head>
    <script type="text/javascript">
    var playerInstance=jwplayer('smthing');
    playerInstance.setup(
        {
        title : '',tracks: [{
            file: '',kind: 'captions','default': true
        }],sources: [{'file':'vidurl','type':'video/mp4'}],image: "imageurl",captions:
            {
            color:'#FFFF00',fontSize:17,backgroundOpacity:50
        },}
    );
    </script>
</head>
</html>

我正在使用 Cheerio 加载 html 但是...我如何真正获得 vidurl?

这是我的 index.js:

const html = ("the scripttag.html")

    const cheerio = require("cheerio");


    let $ = cheerio.load(html)
const scripttag = $.html("head > script")

const title = ...
const srcfiles = ...
const image = ...

现在我得到了脚本标签,但是我如何得到

来自 playerintance.setup 的常量标题 -> 标题

const srcfiles from playerinstance.setup -> 源 -> 文件

来自 playerinstance.setup 的常量图像 -> 图像

解决方法

您可以为此使用正则表达式:

html.match(/\{'file':'(.*?)'/)[1]