PHP 擷取網頁所有串連

來源:互聯網
上載者:User
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'));//IPcurl_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 . ']<br>';}$html = null;

代碼下載:http://files.cnblogs.com/zjfree/php_get_html.rar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.