栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP爬取网页

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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()函数

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);
//如果出现中文乱码使用下面代码`
//$getcontent = iconv("gb2312", "utf-8",$html);echo"".$html."";
  1. fopen()->fread()->fclose()模式  ,目前还没用过,看到了就先记下了

4.使用curl实现(本人一般使用这个)。
使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分号去掉,而且需 要拷贝ssleay32.dll和libeay32.dll到C:WINDOWSsystem32下;Linux下要安装curl扩展。

(.*?)/',$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('//',$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; 
}?>
  1. fsockopen()函数 socket模式(没用过,以后可以试试)
    socket模式能否正确执行,也跟服务器的设置有关系,具体可以通过phpinfo查看服务器开启了哪些通信协议

n";
} else {    $out = "GET / HTTP/1.1rn";    $out .= "Host: t.qq.comrn";    $out .= "Connection: Closernrn";
    fwrite($fp, $out);    while (!feof($fp)) {        echo fgets($fp, 128);
    }
    fclose($fp);
}
  1. 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里面就可以了。

fetch($url);//保存采集内容到$lines_string$lines_string=$snoopy->results;//输出内容,嘿嘿,大家也可以保存在自己的服务器上echo $lines_string;



作者:Sean_Zhou
链接:https://www.jianshu.com/p/d1c65b2c63c6

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/227674.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号