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); 加上这句代码,示意假如要求被重定向时,能够访问到终究的要求页面,不然要求的效果会显现以下内容: <head><title>Object moved</title></head> <body><h1>Object Moved</h1>This object may be found <a href="some link." rel="external nofollow"
以上内容仅供参考!
引荐教程:PHP视频教程
以上就是php怎样抓取网页上的数据的细致内容,更多请关注ki4网别的相干文章!