vendredi 7 juin 2019

How to scrape a specific element of a website using PHP?

I'm trying to scrape a specific element of a website using PHP. I tried a few techniques using simple_html_dom but unsuccessfully. Basically I want to scrape the current variation of btc's price (a percentage) present on so many website.

I tried 2 ways but none of them worked: using simple_html_dom's find() method and getElementbyId(); and basically the same but with a string output at the end with saveXML(). The element that i want is inside <span id="centval"> % </span> so I tried to look for this specific id.

 require_once 'lib/simple_html_dom.php';

 $source_url = 'https://bitcointicker.co/';

 $html_source = file_get_html($source_url);

 $changes = $html_source->find('span')->plaintext;
 $changes->getElementById('centval');

And


function getElementByIdAsString() {
    $pretty = true;
    $url = 'https://bitcointicker.co/';
    $id = "centval";
    $doc = new DOMDocument();
    @$doc->loadHTMLFile($url);
    $element = $doc->getElementById($id);
    echo $doc->saveXML($element);
}

So I expected my page to output the percentage of variation in the btc price but it didn't output anything, still loading, so I guess I'm doing something wrong but I can't see what :x

Aucun commentaire:

Enregistrer un commentaire