PHP擷取網頁所有串連的方法(附demo源碼下載),demo源碼下載
本文執行個體講述了PHP擷取網頁所有串連的方法。分享給大家供大家參考,具體如下:
function getHtml($url, $charset='utf-8'){ $curl = curl_init(); //curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:192.168.168.1', 'CLIENT-IP:192.168.168.1'));//IP curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_REFERER, ""); //來路 $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31'; curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); // 只需返回HTTP header // curl_setopt($curl, CURLOPT_HEADER, 1); // 頁面內容我們並不需要 // curl_setopt($curl, CURLOPT_NOBODY, 1); // 返回結果,而不是輸出它 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $html = curl_exec($curl); //$info = curl_getinfo($curl); //echo var_dump($info); if ($html === false) { //echo "cURL Error: " . curl_error($ch); return ''; } curl_close($curl); if ($charset != 'utf-8') { $html = iconv($charset, "UTF-8", $html); } return $html;}header("Content-type: text/html; charset=utf-8");include('simple_html_dom.php');// 要開啟 extension=php_mbstring.dll//$url = 'http://www.baidu.com/s?wd=kaka';$url = 'http://www.163.com/';$str_html = getHtml($url, 'gbk');$html = str_get_html($str_html);$links = $html->find('a');foreach($links as $link){ $txt = trim($link->plaintext); echo $link->href . '[' . $txt . ']
';}$html = null;
完整執行個體代碼點擊此處本站下載。
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php操作office文檔技巧總結(包括word,excel,access,ppt)》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程式設計有所協助。
您可能感興趣的文章:
- php擷取網頁上所有連結的方法
- php Regex提取網頁超級連結url的函數
- 簡介WordPress中用於擷取首頁和站台連結的PHP函數
- WordPress中擷取頁面連結和標題的相關PHP函數用法解析
- php擷取超連結常值內容的Regex(五種方法)
- phpRegex擷取內容所有連結
- PHP擷取短連結跳轉後的真真實位址和回應標頭資訊的方法
- php 自寫函數代碼 擷取關鍵字 去超連結
http://www.bkjia.com/PHPjc/1117060.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1117060.htmlTechArticlePHP擷取網頁所有串連的方法(附demo源碼下載),demo源碼下載 本文執行個體講述了PHP擷取網頁所有串連的方法。分享給大家供大家參考,具體如下...