PHP 獲取指定URl頁面中所有連結
阿新 • • 發佈:2019-02-02
<?php
//獲取指定URL頁面中所有連結
function get_url_href($url){
$html = file_get_contents($url);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate('/html/body//a');
$arr_urls = array();
for($i=0;$i<$hrefs->length;$i ++){
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
if(substr($url,0,4) == 'http') {
$arr_urls[] = $url;
}
}
return $arr_urls;
}
$arr_urls = get_url_href("http://www.baonahao.com");
echo "<pre/>";
print_r($arr_urls);