Search code examples
phpscreen-scraping

How to create a simple screen scraper in PHP


I am trying to create a simple screen scraper that gets me the price of a specific item. Here is an example of a product I want to get the price from:

https://www.flanco.ro/telefon-mobil-apple-iphone-14-5g-128gb-purple.html

This is the portion of the html code I am interested in: enter image description here

I want to get the '4699' thing.

Here is what I have been trying to do but it does not seem to work:

$html = file_get_contents("https://www.flanco.ro/telefon-mobil-apple-iphone-14-5g-128gb-purple.html");
$doc = new DomDocument();
$doc->loadHtml($html);
$xpath = new DomXPath($doc);
//Now query the document:
foreach ($xpath->query('/<span class="price">[0-9]*\\.[0-9]+/i') as $node) {
    echo $node, "\n";
}

Solution

  • You could just use standard PHP string functions to get the price out of the $html:

    $url   = "https://www.flanco.ro/telefon-mobil-apple-iphone-14-5g-128gb-purple.html";
    $html  = file_get_contents($url);
    $seek  = '<span class="special-price"><span class="price">';
    $end   = strpos($html, $seek) + strlen($seek);
    $price = substr($html, $end, strpos($html, ',', $end) - $end);
    

    Or something similar. This is all the code you need. This code returns:

    4.699
    

    My point is: In this particular case you don't need to parse the DOM and use a regular expression to get that single price.