简体   繁体   中英

Node.js crawler: find a href by text via cheerio

My node app get HTML code from a certain webiste with request library. And by using jQuery (cheerio) I want to find a href to contact section. But then I get only undefined value.

request(options.websiteUrl, (err, response, body) =>{
            if(!err && response.statusCode == 200){

                    let $ = cheerio.load(body); 
                    let contact =  $('a:contains("contact")').attr('href');
                    console.log(contact); //undefined

            }
    });

I've pasted the same jQuery command directly into that website's JS console and it works fine, giving me proper href. What's the difference between regular jQuery and Cheerio that makes me unable to execute that code inside my node app and how can I fix it?

It seems that cheerio is not waiting for the response body javascript to be fully loaded. You could wrap the cheerio code block in a setTimeout to give cheerio time for the body to get loaded.

You can see another similar stack overflow post about this over here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM