简体   繁体   中英

Beginner headache PHP DOMXPath

I try to write a simple PHP to scrape a html page. I don't know why I cannot get the result? Here is some of my PHP code:

//$html , successfuly get the html from "http://m.hkgolden.com/topics.aspx?type=HW" by curl

$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('x', 'http://www.w3.org/1999/xhtml');

$itemList = $xpath->query('//x:div[contains(@class,"TopicBox_Details")]/a');

var_dump($itemList); // it show --> object(DOMNodeList)#4 (0) { }

foreach ($itemList as $item){
        $this->child_urls[] = $item->getElementsByTagName('a')->item(0)->getAttribute('href');
                }

var_dump($this->child_urls); //it show --> array(0) { } 

the same xpath query is work in the firefox XPath Checker, but the same query cannot work in PHP. am I did something wrong? Firefox XPath检查器

您还应该将命名空间添加到XPath中的a元素-命名空间被继承:

//x:div[contains(@class,"TopicBox_Details")]/x:a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM