Skip to content
Advertisement

How to extract price from given url using Xpath?

From a given url I want to extract some detail from it like title, price.

For the title, it works fine with this code:

<?php
    $getURL = file_get_contents('http://realestate.com.kh/residential-for-sale-in-phnom-penh-phnom-penh-bkk-1-1-bed-apartment-2386341');
    $dom = new DOMDocument();
    @$dom->loadHTML($getURL);
    $xpath = new DOMXPath($dom);

    echo $xpath->evaluate('//title')->item(0)->nodeValue."n";
    
?>

But I wonder how to extract price($) using xpath? Is there any way to solve this?

Advertisement

Answer

echo $xpath->evaluate('//p[contains(@class,"price")]')->item(0)->nodeValue;

This will find <p class="price right s-cf">US$160000</p>

The contains is doing a wildcard search on the class attribute of the element.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement