D
D
Daniil Sukhikh2021-07-07 19:43:07
PHP
Daniil Sukhikh, 2021-07-07 19:43:07

How to find elements via DOMXpath?

I want to parse ads. I was able to get the entire ad page with curl and am now trying to collect all the ad titles from the page. But doesn't work. When I do var_dump the result is: Code:
object(DOMNodeList)#10 (1) { ["length"]=> int(0) }

$link = $this->model->parseLink('https://www.olx.ua/elektronika/tv-videotehnika/odessa/?search%5Bprivate_business%5D=private');
        $dom = new \DOMDocument($link);
        $xpath = new \DOMXPath($dom);
        $elements = $xpath->query('/html/body/div[1]/div[4]/section/div[3]/div/div[1]/table[2]/tbody/tr/td/div/table/tbody/tr[1]/td[2]/div/h3/a/strong');

The parseLink function, everything is fine with it, but I will attach the code:
public function parseLink($link)
    {
        $curl = curl_init($link);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($curl);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2021-07-07
@danchikraw

$html = file_get_contents('https://www.olx.ua/elektronika/tv-videotehnika/odessa/?search%5Bprivate_business%5D=private');

libxml_use_internal_errors(true);
$dom = new \DOMDocument();
$dom->loadHTML($html);
$xpath = new \DOMXPath($dom);
$elements = $xpath->query('/html/body/div[1]/div[4]/section/div[3]/div/div[1]/table[2]/tbody/tr/td/div/table/tbody/tr[1]/td[2]/div/h3/a/strong');

foreach ($elements as $title) {
    echo "$title->nodeValue\n";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question