<?php 
$url="http://www.baidu.com";
// 获取链接的HTML代码
$html=file_get_contents($url);
// 创建DOMdocument对象
$dom=new DOMdocument();
@$dom->loadHTML($html);
$xpath=new DOMxpath($dom);
// $xpath->evaluate对给定的XPath表达式并返回一个类型的结果,如果可能的话
$hrefs = $xpath->evaluate('/html/body//a');
for($i=0;$i<$hrefs->length;$i++){
	$href=$hrefs->item($i);
	$url=$href->getAttribute("href");
	// echo $url.'<br>';
	// 保留以http开头的链接
	if(substr($url, 0, 4) == 'http')
	echo $url.'<br >';
}
?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注