• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP如何实现爬取网页的功能

php 搞代码 4年前 (2022-01-21) 18次浏览 已收录 0个评论

本篇文章给大家分享的是关于PHP如何实现爬取网页的功能,内容很不错,有需要的朋友可以参考一下,希望可以帮助到大家。

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

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

1.file()函数

2.file_get_contents()函数

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

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

5.fsockopen()函数 socket模式

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

7.file()函数

<?php//定义url$url='[http://t.qq.com](http://t.qq.com/)';//fiel函数读取内容数组$lines_array=file($url);//拆分数组为字符串$lines_string=implode('',$lines_array);//输出内容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都不能打开远程文件。

$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)";$html=file_get_contents($url);/<i>本文@来#源gaodai$ma#com搞$$代**码网</i><strong>搞代gaodaima码</strong>/如果出现中文乱码使用下面代码`//$getcontent = iconv("gb2312", "utf-8",$html);echo"<textarea style='width:800px;height:600px;'>".$html."</textarea>";

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

<?php//定义url$url='[http://t.qq.com](http://t.qq.com/)';//fopen以二进制方式打开 $handle=fopen($url,"rb");//变量初始化$lines_string="";//循环读取数据do{$data=fread($handle,1024);  if(strlen($data)==0) {`break; } $lines_string.=$data;}while(true);//关闭fopen句柄,释放资源fclose($handle);//输出内容echo $lines_string;

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

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

<?phpheader("Content-Type: text/html;charset=utf-8");date_default_timezone_set('PRC');$url = "https://***********ycare";//要爬取的网址$res = curl_get_contents($url);//curl封装方法preg_match_all('/<script>(.*?)<\/script>/',$res,$arr_all);//这个网页中数据通过js包过来,所以直接抓js就可以preg_match_all('/"id"\:"(.*?)",/',$arr_all[1][1],$arr1);//从js块中匹配要的数据$list = array_unique($arr1[1]);//(可省)保证不重复//以下则是同理,循环则可for($i=0;$i<=6;$i=$i+2){  $detail_url = 'ht*****em/'.$list[$i];  $detail_res = curl_get_contents($detail_url);  preg_match_all('/<script>(.*?)<\/script>/',$detail_res,$arr_detail);  preg_match('/"desc"\:"(.*?)",/',$arr_detail[1][1],$arr_content);  ***    ***    ***  $ret=curl_post('http://**********cms.php',$result);//此脚本未放在服务器上,原因大家懂就好哈。}function curl_get_contents($url,$cookie='',$referer='',$timeout=300,$ishead=0) {  $curl = curl_init();  curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);  curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);  curl_setopt($curl, CURLOPT_URL,$url);  curl_setopt($curl, CURLOPT_TIMEOUT,$timeout);  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');  if($cookie)  {    curl_setopt( $curl, CURLOPT_COOKIE,$cookie);  }  if($referer)  {    curl_setopt ($curl,CURLOPT_REFERER,$referer);  }  $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;  if ($ssl)  {    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  }  $res = curl_exec($curl);  return $res;  curl_close($curl);}//curl post数据到服务器function curl_post($url,$data){  $ch = curl_init();  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  //curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  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');  curl_setopt($ch,CURLOPT_URL,$url);  curl_setopt($ch,CURLOPT_POST,true);  curl_setopt($ch,CURLOPT_POSTFIELDS,$data);  $output = curl_exec($ch);  curl_close($ch);  return $output; }?>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP如何实现爬取网页的功能

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址