0

I am using simple dom parser (php) to extract the dollar price from: http://www.lanacion.com.ar/dolar-tarjeta-t50462

<div class="numero floatFix">
     <p class="peso">$</p>
     <p id="dTarjeta" class="valor x35">
         12,920
     </p>

But I get the followng character "-" instead of "12,290". The code I use is:

$url_to_traverse = 'http://www.lanacion.com.ar/dolar-tarjeta-t50462';
$html = file_get_html($url_to_traverse);
foreach($html->find('#dTarjeta') as $element)
{
   var_dump($element->text());
}

What I am doing wrong ?. Hope there is a solution coz I 've try a lot of libraries...

AgusDesign
  • 57
  • 2
  • 11
  • Why scraping? Try a web service for currency rates, e.g. https://openexchangerates.org/ – lxg Nov 05 '15 at 13:11

1 Answers1

2

This is because, the data in #dTarjeta is fed using javascript after page load. Ie, when the file_get_html function reads the url, thsi is what it looks like :

    <p id="dTarjeta" class="valor x35">-</p>

It is clear that, after the page is loaded, the text in #dTarjeta is changed using javascript using an ajax call or something similar ( Havent checked it ).

So the answer to your question is, you are getting the correct output, since PHP is not able to render pages with client javascript, and it only renders what the server sends in first.

I would recommend you to use Phantomjs or simialr technologies to scrape these kind of pages.

Verify this by using : view-source:http://www.lanacion.com.ar/dolar-tarjeta-t50462

Additionally you can use this stackoverflow question : Scrape web pages in real time with Node.js for reference.

Additonally, if you prefer to use other sources for getting currency values, consider using

  1. http://fixer.io/ - Free JSON APIs.
  2. https://openexchangerates.org/ - Same
  3. Yahoo Finance APIs

This link will help you to handle JSON APIs using PHP. Get data from JSON file with PHP

Community
  • 1
  • 1
Sak90
  • 600
  • 6
  • 19
  • Thankx you very much !. I was thinking in finding sites wich offer api's rather than scraping... Won't use node but I plan to consider ir using... Do you know a good hosting site ?... The one I have doesn't support; vaa I should ask... Thankx – AgusDesign Nov 05 '15 at 13:14
  • Sorry for being late, I was away. What hosting site you need ? If you want real time price data, there are a couple of APIs. I will edit the answer accordingly. If the answer helps, mark it as the correct answer :-) So that it will be easier for others to save time. – Sak90 Nov 05 '15 at 14:02