php – 无法从网站上抓取内容
发布时间:2020-12-13 13:14:15 所属栏目:PHP教程 来源:网络整理
导读:我试图从网站上删除一些内容,但下面的代码不起作用(没有显示任何输出). 这是代码 $url="some url";$otherHeaders=""; //here i am using some other headers like content-type,userAgent,etcsome curl to get the webpage.....curl_setopt($ch,CURLOPT_RETU
我试图从网站上删除一些内容,但下面的代码不起作用(没有显示任何输出).
这是代码 $url="some url"; $otherHeaders=""; //here i am using some other headers like content-type,userAgent,etc some curl to get the webpage ... .. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $content=curl_exec($ch);curl_close($ch); $page=new DOMDocument(); $xpath=new DOMXPath($page); $content=getXHTML($content); //this is a tidy function to convert bad html to xhtml $page->loadHTML($content); // its okay till here when i echo $page->saveHTML the page is displayed $path1="//body/table[4]/tbody/tr[3]/td[4]"; $path2="//body/table[4]/tbody/tr[1]/td[4]"; $item1=$xpath->query($path1); $item2=$xpath->query($path2); echo $item1->length; //this shows zero echo $item2->length; //this shows zero foreach($item1 as $t) echo $t->nodeValue; //doesnt show anything foreach($item2 as $p) echo $p->nodeValue; //doesnt show anything 我确定上面的xpath代码有问题. xpath是正确的.我用FirePath(firefox插件)检查了上面的xpath.我知道我错过了一些非常愚蠢的东西,但我无法理解.请帮忙.
是的,你缺少一些非常基本的东西:它是XHTML,所以你必须注册(并使用!)正确的
namespace才能获得结果.
$xpath->registerNamespace('x','http://www.w3.org/1999/xhtml'); $path1="//x:body/x:table[4]/x:tbody/x:tr[3]/x:td[4]"; $path2="//x:body/x:table[4]/x:tbody/x:tr[1]/x:td[4]"; $item1=$xpath->query($path1); $item2=$xpath->query($path2); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |