php中抓取網頁內容的實例詳解
方法一:
使用file_get_contents方法實現
$url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml"; $html = file_get_contents($url); //如果出現中文亂碼使用下面代碼 //$getcontent = iconv("gb2312", "utf-8",$html); echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";代碼很簡單,一看就懂,不解釋了。
方法二:
使用curl實現
$url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml"; $ch = curl_init();curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_returntransfer, 1);curl_setopt($ch, curlopt_connecttimeout, 10);curl_setopt($ch, curlopt_followlocation, 1);$html = curl_exec($ch);curl_close($ch);echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";curl_setopt($ch, curlopt_followlocation, 1);加上這句代碼,表示如果請求被重定向時,可以訪問到最終的請求頁面,不然請求的結果會顯示如下內容:
相關學習推薦:php編程(視頻)
百度云服務器需要轉移備案嗎數字域名價值高低?數字域名價值怎樣?小程序好推廣嗎怎么購買美國虛擬主機電腦怎么設置網絡訪問權限保護共享文件安全CTO高端對話,華云數據與Veeam助力中國企業平穩上云如何提高網站速度有利于SEO蜘蛛抓取域名公司深圳御羽文化傳播有限公司