当前位置:首页 > PHP教程 > php应用 > 列表

掌握PHP爬取网页的主要方法

发布:smiling 来源: PHP粉丝网  添加日期:2022-07-20 16:50:34 浏览: 评论:0 

主要流程就是获取整个网页,然后正则匹配(关键的)。

PHP抓取页面的主要方法,有几种方法是网上前辈的经验,现在还没有用到的,先存下来以后试试。

1.file()函数

2.file_get_contents()函数

3.fopen()->fread()->fclose()模式

4.curl方式 (本人主要用这个)

5.fsockopen()函数 socket模式

6.插件(如:http://sourceforge.net/projects/snoopy/)

7.file()函数

  1. <?php 
  2.  
  3. //定义url 
  4.  
  5. $url='[http://t.qq.com](http://t.qq.com/)';//fiel函数读取内容数组 
  6.  
  7. $lines_array=file($url);//拆分数组为字符串 
  8.  
  9. $lines_string=implode('',$lines_array);//输出内容 
  10.  
  11. echo $lines_string;  

2.使用file_get_contents方法实现,比较简单。

使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置 allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。

  1. $url="[http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml](http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml)"; 
  2.  
  3. $html=file_get_contents($url); 
  4.  
  5. //如果出现中文乱码使用下面代码` 
  6.  
  7. //$getcontent = iconv("gb2312", "utf-8",$html); 
  8.  
  9. echo"<textarea style='width:800px;height:600px;'>".$html."</textarea>"; 

3.fopen()->fread()->fclose()模式 ,目前还没用过,看到了就先记下了

  1. <?php 
  2.  
  3. //定义url 
  4.  
  5. $url='[http://t.qq.com](http://t.qq.com/)';//fopen以二进制方式打开  
  6.  
  7. $handle=fopen($url,"rb");//变量初始化 
  8.  
  9. $lines_string="";//循环读取数据 
  10.  
  11. do{ 
  12.  
  13. $data=fread($handle,1024);   
  14.  
  15. if(strlen($data)==0) {` 
  16.  
  17. break;  
  18.  
  19. }  
  20.  
  21. $lines_string.=$data; 
  22.  
  23. }while(true);//关闭fopen句柄,释放资源 
  24.  
  25. fclose($handle);//输出内容 
  26.  
  27. echo $lines_string; 

4.使用curl实现(本人一般使用这个)。

使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分号去掉,而且需 要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩展。

  1. <?php 
  2.  
  3. header("Content-Type: text/html;charset=utf-8"); 
  4.  
  5. date_default_timezone_set('PRC'); 
  6.  
  7. $url = "https://***********ycare";//要爬取的网址 
  8.  
  9. $res = curl_get_contents($url);//curl封装方法 
  10.  
  11. preg_match_all('/<script>(.*?)<\/script>/',$res,$arr_all);//这个网页中数据通过js包过来,所以直接抓js就可以 
  12.  
  13. preg_match_all('/"id"\:"(.*?)",/',$arr_all[1][1],$arr1);//从js块中匹配要的数据 
  14.  
  15. $list = array_unique($arr1[1]);//(可省)保证不重复 
  16.  
  17. //以下则是同理,循环则可 
  18.  
  19. for($i=0;$i<=6;$i=$i+2){ 
  20.  
  21.   $detail_url = 'ht*****em/'.$list[$i]; 
  22.  
  23.   $detail_res = curl_get_contents($detail_url); 
  24.  
  25.   preg_match_all('/<script>(.*?)<\/script>/',$detail_res,$arr_detail); 
  26.  
  27.   preg_match('/"desc"\:"(.*?)",/',$arr_detail[1][1],$arr_content); 
  28.  
  29.   *** 
  30.  
  31.     *** 
  32.  
  33.     *** 
  34.  
  35.   $ret=curl_post('http://**********cms.php',$result);//此脚本未放在服务器上,原因大家懂就好哈。 
  36.  
  37. } 
  38.  
  39. function curl_get_contents($url,$cookie='',$referer='',$timeout=300,$ishead=0) { 
  40.  
  41.   $curl = curl_init(); 
  42.  
  43.   curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); 
  44.  
  45.   curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1); 
  46.  
  47.   curl_setopt($curl, CURLOPT_URL,$url); 
  48.  
  49.   curl_setopt($curl, CURLOPT_TIMEOUT,$timeout); 
  50.  
  51.   curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'); 
  52.  
  53.   if($cookie) 
  54.  
  55.   { 
  56.  
  57.     curl_setopt( $curl, CURLOPT_COOKIE,$cookie); 
  58.  
  59.   } 
  60.  
  61.   if($referer) 
  62.  
  63.   { 
  64.  
  65.     curl_setopt ($curl,CURLOPT_REFERER,$referer); 
  66.  
  67.   } 
  68.  
  69.   $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE; 
  70.  
  71.   if ($ssl) 
  72.  
  73.   { 
  74.  
  75.     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
  76.  
  77.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
  78.  
  79.   } 
  80.  
  81.   $res = curl_exec($curl); 
  82.  
  83.   return $res; 
  84.  
  85.   curl_close($curl); 
  86.  
  87. } 
  88.  
  89. //curl post数据到服务器 
  90.  
  91. function curl_post($url,$data){ 
  92.  
  93.   $ch = curl_init(); 
  94.  
  95.   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  96.  
  97.   //curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1); 
  98.  
  99.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  100.  
  101.   curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'); 
  102.  
  103.   curl_setopt($ch,CURLOPT_URL,$url); 
  104.  
  105.   curl_setopt($ch,CURLOPT_POST,true); 
  106.  
  107.   curl_setopt($ch,CURLOPT_POSTFIELDS,$data); 
  108.  
  109.   $output = curl_exec($ch); 
  110.  
  111.   curl_close($ch); 
  112.  
  113.   return $output;  
  114.  
  115. } 
  116.  
  117. ?> 

5.fsockopen()函数 socket模式(没用过,以后可以试试)

socket模式能否正确执行,也跟服务器的设置有关系,具体可以通过phpinfo查看服务器开启了哪些通信协议

  1. <?php 
  2.  
  3. $fp = fsockopen("t.qq.com", 80, $errno, $errstr, 30); 
  4.  
  5. if (!$fp) { 
  6.  
  7.   echo "$errstr ($errno)<br />\n"; 
  8.  
  9. } else { 
  10.  
  11.   $out = "GET / HTTP/1.1\r\n"; 
  12.  
  13.   $out .= "Host: t.qq.com\r\n"; 
  14.  
  15.   $out .= "Connection: Close\r\n\r\n"; 
  16.  
  17.   fwrite($fp, $out); 
  18.  
  19.   while (!feof($fp)) { 
  20.  
  21.     echo fgets($fp, 128); 
  22.  
  23.   } 
  24.  
  25.   fclose($fp); 
  26.  
  27. } 

6.snoopy插件,最新版本是Snoopy-1.2.4.zip Last Update: 2013-05-30,推荐大家使用。

使用网上非常流行的snoopy来进行采集,这是一个非常强大的采集插件,并且它的使用非常方便,你也可以在里面设置agent来模拟浏览器信息。

说明:设置agent是在 Snoopy.class.php 文件的第45行,请在该文件中搜索 “var 公式输入有误_SERVER['HTTP_USER_AGENT']; 可以得到浏览器信息,将echo出来的内容复制到agent里面就可以了。

  1. <?php 
  2.  
  3. //引入snoopy的类文件 
  4.  
  5. require('Snoopy.class.php'); 
  6.  
  7. //初始化snoopy类 
  8.  
  9. $snoopy=new Snoopy; 
  10.  
  11. $url="[http://t.qq.com](http://t.qq.com/)"; 
  12.  
  13. //开始采集内容` 
  14.  
  15. $snoopy->fetch($url); 
  16.  
  17. //保存采集内容到$lines_string 
  18.  
  19. $lines_string=$snoopy->results; 
  20.  
  21. //输出内容,嘿嘿,大家也可以保存在自己的服务器上 
  22.  
  23. echo $lines_string;

Tags: PHP爬取网页

分享到: