T
T
tltary2017-11-09 19:14:19
PHP
tltary, 2017-11-09 19:14:19

How to parse an element using xpath?

There is such a code

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "адрес сайта");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU; rv:1.7.12) Gecko/20050919 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
 
$domDocument = new DOMDocument();
@$domDocument->loadHTML($content);
 
$xPath = new DOMXPath($domDocument);
$pElements = $xPath->query('//html/body/div[1]/div[3]/div/div/table/tbody/tr[15]/td');
$pElements2 = $xPath->query('//html/body/div[1]/div[3]/div/div/table/tbody/tr[18]/td');
$pElements3 = $xPath->query('//html/body/div[1]/div[3]/div/div/table/tbody/tr[3]/td');

 
if ($pElements->length > 0) {
    foreach ($pElements as $pElement) {
        $result[] = $pElement->nodeValue."\n";
    }
}

if ($pElements2->length > 0) {
    foreach ($pElements2 as $pElement) {
        $result2[] = $pElement->nodeValue."\n";
    }
}
if ($pElements3->length > 0) {
    foreach ($pElements3 as $pElement) {
        $result3[] = $pElement->nodeValue."\n";
    }
}
 
 
file_put_contents("article.txt", $result);
file_put_contents("article1.txt", $result2);
file_put_contents("article2.txt", $result3);

But I can’t understand how xpath works, no matter how much I try, nothing happens and the data is not parsed.
Also, it is necessary that not one url be taken, but an array
Which direction is better to look at?
I know that the code is not very good, but I need a quick and easy way to parse certain data

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2017-11-09
@dimonchik2013

if you see y/div [1] / in xpath - hit your hands until you learn how to write valid expressions
in general, css selectors are easier and often enough in 80% of cases

I
Ilya, 2017-11-09
@glebovgin

1. Make sure you receive the document accurately. echo $content;will help.
2. Try using xpath to take some simple element, for example title

$el = $xPath->query('//title');
echo $el->item(0)->nodeValue;

3. Reduce xpath queries to more understandable and simple ones. For example, if the table you need has a class or id, then just contact
You can almost always find a way to get to the element faster than going from the root element //html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question