[英]Beginner headache PHP DOMXPath
I try to write a simple PHP to scrape a html page. 我尝试编写一个简单的PHP来刮取html页面。 I don't know why I cannot get the result?
我不知道为什么无法得到结果? Here is some of my PHP code:
下面是我的一些PHP代码:
//$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. 在firefox XPath Checker中可以使用相同的xpath查询,但是在PHP中不能使用相同的查询。 am I did something wrong?
我做错什么了吗?
您还应该将命名空间添加到XPath中的a
元素-命名空间被继承:
//x:div[contains(@class,"TopicBox_Details")]/x:a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.