How to scrape web data using Cheerio?

667 Views Asked by At

i have solution want grab some content from url.

I have code in 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);
    }
});

It return 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>

I want output as:

{
    http://abc[dot]com/video34345453.mp4,
    http://abc[dot]com/video43243234.mp4
}

UPDATE: after found on internet, i am try ti fix:

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

thankyou any solution.

2

There are 2 best solutions below

1
On BEST ANSWER

In your if statement, try something like:

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('}');
0
On

You can access script content as follows:

1) $('script').get()[0].attribs[//attribute name]

2) let $ = cheerio.load(html, {xmlMode: false});