Javascript 如果类名包含空格,则在DOMXpath中使用查询将不起作用
我正在尝试使用php机器人从外部站点提取链接。链接在里面Javascript 如果类名包含空格,则在DOMXpath中使用查询将不起作用,javascript,domdocument,domxpath,Javascript,Domdocument,Domxpath,我正在尝试使用php机器人从外部站点提取链接。链接在里面 <td class=" title-col"> <a href="http://examplenews101.com/post1">News 1</a> </td> 但是我需要按类查询 谢谢我发现这是由javascript生成的属性造成的 function crawl_page($url, $depth = 5) { static $seen = array(); if (isset(
<td class=" title-col"> <a href="http://examplenews101.com/post1">News 1</a> </td>
但是我需要按类查询
谢谢我发现这是由javascript生成的属性造成的
function crawl_page($url, $depth = 5) {
static $seen = array();
if (isset($seen[$url]) || $depth === 0) {
return;
}
$seen[$url] = true;
$dom = new DOMDocument('1.0');
//als tried true , but no change in results
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($url);
$xpath = new DOMXpath($dom);
$td = $xpath->query('//td[contains(concat(" ", normalize-space(@class), " "), "title-col")]');
// also tried this, but not working
//$td = $xpath->query('//td[contains(@class,"title-col")]');
//I only get values when I use this
//$td = $dom->getElementsByTagName('td');
foreach( $td as $t ) {
$anchors = $t->getElementsByTagName('a');
foreach ($anchors as $element) {
$href = $element->getAttribute('href');
if (0 !== strpos($href, 'http')) {
$path = '/' . ltrim($href, '/');
if (extension_loaded('http')) {
$href = http_build_url($url, array('path' => $path));
}
else {
$parts = parse_url($url);
$href = $parts['scheme'] . '://';
if (isset($parts['user']) && isset($parts['pass'])) {
$href .= $parts['user'] . ':' . $parts['pass'] . '@';
}
$href .= $parts['host'];
if (isset($parts['port'])) {
$href .= ':' . $parts['port'];
}
$href .= $path;
}
}
crawl_page($href, $depth - 1);
}
}
echo "URL:" . $url . "<br/>";
}
$td = $dom->getElementsByTagName('td');