Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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
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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question