php xpath 屏幕抓取问题
好吧,这看起来很简单,但我根本无法让这个编码工作...
$html = new DOMDocument();
@$html->loadHtmlFile('http://www.groupon.com/deals/the-newfoundland-shop');
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "/html/body/div[@id='global_container']/div[@id='main']/div[@id='content']/div/div[1]/div[2]/div[@id='number_sold_container']/table[1]/tbody/tr/td" );
foreach ($nodelist as $n){
echo $n->nodeValue."\n";
}
$buys = "$n->nodeValue";
请在我失去它之前提供帮助...
谢谢
Ok, this seems so simple but I can't get this coding working at all...
$html = new DOMDocument();
@$html->loadHtmlFile('http://www.groupon.com/deals/the-newfoundland-shop');
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "/html/body/div[@id='global_container']/div[@id='main']/div[@id='content']/div/div[1]/div[2]/div[@id='number_sold_container']/table[1]/tbody/tr/td" );
foreach ($nodelist as $n){
echo $n->nodeValue."\n";
}
$buys = "$n->nodeValue";
Please help before I loose it...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试对 XPath 使用此语法 -
/xhtml:html/xhtml:body/xhtml:div[@id='global_container'] ...
如果没有帮助检查您的 XPath 是否正常(它真的很长)
Try to use this syntax for XPath -
/xhtml:html/xhtml:body/xhtml:div[@id='global_container'] ...
If dont helped check if your XPath is ok ( it is really long )
第一个常见问题解答:页面看起来正确的 XHTML,因此您需要使用一些前缀注册 XHTML 命名空间 URI
http://www.w3.org/1999/xhtml
,并在 XPath 表达式中使用该前缀。第二个常见问题解答:您应该注意原始源没有
tbody
元素。这是由浏览器添加的。First FAQ: the page looks proper XHTML, so you need to register the XHTML namespace URI
http://www.w3.org/1999/xhtml
with some prefix, and use that prefix in your XPath expression.Second FAQ: you should note that original source has not
tbody
element. This is added by the browser.