voku / simple_html_dom

📜 Modern Simple HTML DOM Parser for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get recommendation rating from sidebar in Yahoo Finance

corn-flour opened this issue · comments

I am trying to scrape the recommendation rating that is provided in Yahoo Finance.
Example page: https://finance.yahoo.com/quote/BYND/analysis?p=BYND
On the side of the page you can find the recommendation rating as shown below
image
The structure of the part looks something like this:
image
When I try to get $html->find('.YDC-Col2'), I instead got some JS code:
image
When I try to see the value of $html->plaintext, I could see that the value I am looking for is under "recommendationMean"
image

Hence, I want to know how I could get this value out of $html. Thank you so much in advance.

Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details.

Update: the reason I got JS code was because I mistakenly return the plainText of the result rather than the innerText. If i do

$result = $html->find('YDC-Col2');
return $result->innerText;

I instead got this:
image
I still am not able to get the value I need though.

@bte234 You need to fetch the code with a headless browser first, because the site is using JavaScript to render the page. (

$lall = $document->findOne('*[data-test="rec-rating-txt"]');
)

But sometimes everything looks like a nail because we have the hammer:

<?php

$json = file_get_contents('https://query2.finance.yahoo.com/v11/finance/quoteSummary/BYND?formatted=true&crumb=&lang=en-US&region=US&modules=financialData&corsDomain=finance.yahoo.com');
$array = json_decode($json, true);

echo $array['quoteSummary']['result'][0]['financialData']['recommendationMean']['raw'];

Happy Coding: 👍

Thank you so much for your response. I was not aware of the API request for that one!