简体   繁体   中英

Scraping using cherio and request in node to get yahoo finance info

I am trying to scrape stock information from yahoo finance using cheerio and request in node.

I found that the price has an attribute data-reactid and tried to get it using that but I get a function back not a string when I run it.

const request = require("request");
const cheerio = require("cheerio");

request("https://finance.yahoo.com/quote/%5EIXIC?p=^IXIC",(err,res,html)=> 
{
if(err){console.log("Error");}
else{
    console.log(res.statusCode);
    var $ = cheerio.load(html);
  var price = $("span[data-reactid='34']").text.toString();
   console.log(price);
}
});

Use .text() instead of .text . text is a method of cheerio object.

var price = $("span[data-reactid='34']").text().toString();

If you try rectifying the following line in your script, you should get the result.

var price = $("#quote-market-notice").parent().children('span').first().text();

Output at this moment:

8,164.00

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