简体   繁体   English

使用cherio进行刮擦并在节点中请求获取雅虎财务信息

[英]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. 我试图从使用cheerio和节点请求的雅虎财务中获取股票信息。

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. 我发现价格有一个属性data-reactid并尝试使用它,但是当我运行它时,我得到的函数不是字符串。

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()代替.text text is a method of cheerio object. textcheerio对象的一种方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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