我正在尝试列出页面上的所有链接和名称。我一直在获取以下代码的银行输出
$url="http://www.ciim.in/top-pr-dofollow-social-bookmarking-sites-list-2016";
$html = file_get_contents($url);
节点部分为
$nodes = $my_xpath->query( '//table[@class="social_list"]/tbody/tr' );
foreach( $nodes as $node )
{
$title = $my_xpath->evaluate( 'td[1]/a"]', $node );
$link = $my_xpath->evaluate( 'td[1]/a/@href"]', $node );
echo $title.",".$link."<br>";
}
注意,站点上的右键单击被禁用,我使用开发人员工具来检查chrome 中元素的代码
查询
$nodes = $xpath->query('//table[@class="social_list"]/tbody/tr/td/a');
在foreach内部获取标题和URL
$title = $node->textContent;
$href = $node->getAttribute('href');
编辑:我已经测试了这个代码来检索整个表的
//Query from parent div
$nodes = $xpath->query('//div[@class="table_in_overflow"]');
foreach ($nodes as $node) {
$a = $node->getElementsByTagName("a");
foreach($a as $item) {
$href = $item->getAttribute("href");
$text = $item->nodeValue;
}
}
在选择器'td[1]/a"]'
和'td[1]/a/@href"]'
的末尾有尾随的"]
,所以将它们更改为td[1]/a
和td[1]/a/@href
。
此外,您可以通过只选择带有td
和a
的tr
来改进您的xpath,因此这将忽略没有链接的标头。
'//table[@class="social_list"]/tbody/tr[td/a]'
这将比CCD_ 9 更有效