php如何抓取网页内容,php如何抓取网页数据?
php抓取網頁數據header("Content-type: text/html; charset=utf-8");
//$url = "https://www.cnblogs.com/chenliyang/p/6554647.html";
//$html = file_get_contents($url);
如果出現中文亂碼使用下面代碼
$getcontent = iconv("gb2312", "utf-8",$html);
//echo "".$html.""; //獲取整個內容
第一種 file_get_contents 抓取
file_get_contents() 把整個文件讀入一個字符串中。
該函數是用于把文件的內容讀入到一個字符串中的首選方法。如果服務器操作系統支持,還會使用內存映射技術來增強性能。$url = 'http://www.baidu.com'; //這兒填頁面地址
$info=file_get_contents($url);
//preg_match('|
(.*?)|i',$info,$m);$m1=preg_match('|
(.*?)|',$info,$m);//var_dump($m1);
echo $m[1]; //獲取標題
echo '
';
第二種 curl抓取,$url = "http://www.baidu.com";
$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);
$m1=preg_match('|
(.*?)|',$html,$ms);//echo "".$html.""; //獲取整個內容
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
echo $ms[1]; //獲取標題
echo '
';
更多相關知識,請訪問 PHP中文網!!
本文原創發布php中文網,轉載請注明出處,感謝您的尊重!
總結
以上是生活随笔為你收集整理的php如何抓取网页内容,php如何抓取网页数据?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php swoole编译,php+swo
- 下一篇: php在一定范围内去随机整数,php-如