Answer the question
In order to leave comments, you need to log in
Why doesn't xpath work?
I want to parse a table, code
$fileByUrl = 'http://w1.c1.rada.gov.ua/pls/z7503/a002';
$referer = 'http://rada.gov.ua/';
$ch=curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, "Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.9.168 Version/11.51");
curl_setopt($ch, CURLOPT_URL, $fileByUrl);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
$str = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$code = $info['http_code'];
if($code == 200){
$doc = new DOMDocument;
$doc->load($str);
$xpath = new DomXPath($doc);
$res = $xpath->query('//*[@id="content-all"]/div[2]/div/table/tbody/tr[3]');
foreach($res as $obj) {
echo $obj->nodeValue;
}
Answer the question
In order to leave comments, you need to log in
First of all - because of this:
"load" is for loading files and as a parameter it needs to be given the path to the file. If you want to load a string, you need to use the "loadHTML" function.
Then you will get a bunch of warnings. If a message appears saying that there are misunderstandings with the encoding, you can get rid of it by correcting the line with loadHTML:
In addition to the line about the encoding, there will be a bunch of warnings, like:
Warning: DOMDocument::loadHTML(): Opening and ending tag mismatch: li and div in Entity
Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity
Warning: DOMDocument::loadHTML(): Opening and ending tag mismatch: td and b in Entity
$res = $xpath->query('.//*');
foreach($res as $obj) {
echo $obj->getNodePath() . "\n\r";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question