javascriptgoogle-apps-scriptweb-scrapingcheerioyahoo-finance

Scraping Yahoo Finance Summary Table with Cheerio


I'm trying to scrape "1y Target Est, 1,140.21" from Yahoo Finance Summary Table, as marked in red.

enter image description here

I tried this code, but am getting no data. Inspecting the tag, I see "1,1140.21" is under <td class="Ta(end) Fw(600) Lh(14px)" data-test="ONE_YEAR_TARGET_PRICE-value">1,140.21</td>. So I thought this code should work, but doesn't. What should I change?

function test() {

  const url = 'https://finance.yahoo.com/quote/NVDA/'
  const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getContentText();
  const $ = Cheerio.load(res);
  const targetPrice = $('td[data-test="ONE_YEAR_TARGET_PRICE-value"]').text().toString();
  console.log(targetPrice)
}

Solution

  • I'm not sure where you're getting your selector from (I don't see it on the page at all), but this works for me:

    function test() {
      const url = "<Your URL>";
      const response = UrlFetchApp.fetch(url);
      const $ = Cheerio.load(response.getContentText());
      const targetPrice = $('[data-field="targetMeanPrice"]').text();
      console.log(targetPrice); // => 1,140.21
    }
    

    Should pulling it from the HTML like this fail, there's also a raw JSON payload in a <script type="application/json" data-sveltekit-fetched data-url="..."> tag you can use.

    See also Parsing Yahoo finance data