我尝试编写一个简单的PHP来抓取html页面。 我不知道为什么我不能得到结果? 下面是我的一些PHP代码:初学者头疼PHP DOMXPath
//$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) { }
相同XPath查询是在Firefox的XPath的检查工作,但相同的查询不能在PHP工作。我做错了什么?
什么是'$这个 - > child_xpath'价值? – xdazz 2012-08-02 10:08:25
'// x:div [contains(@class,“TopicBox_Deta ils”)]/a'无论如何,我已经在问题中替换了它。 – 2012-08-02 10:18:16
命名空间。命名空间。命名空间。 :)但我时间不多,所以我只能离开笔记。 – hakre 2012-08-02 10:20:10