采集之页面乱码及Curl模拟post请求

      身为为一个编程人员采集怎么能不会采集虽说有些不太好但在编程时我们难道自己去写或者找数据素材?

【一】采集的素材时候会出现乱码现象

基本上一小网站我们使用file_get_contents()或者simplexml_load_file()这两个函数就搞定了。

可是有时使用file_get_contents或者simplexml_load_file采集的素材时候会出现乱码现象。


通常情况,乱码有一下几种可能:


1、面能获取过来,只是内容乱码而已

这种问题最简单,我们采用iconv()和mb_convert_encoding()函数进行转码即可


2、当内容都无法正常获取的时候,分两种情况第一种是防盗链,第二种是页面需要解压

2.1、防盗链情况下解决办法比较简单,模拟浏览器就可以了

    <?php  
        header('content-type:text/html;charset=utf-8');  
        $url="http://www.sohu.com/";  
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');  
        $html=file_get_contents($url);  
        //echo $html;  
        echo mb_convert_encoding($html,'utf8','gbk');  

2.2、需要解压时,我们可以用以下方式

    <?php  
    header("content-type:text/html;charset=utf-8");  
    $url="http://wthrcdn.etouch.cn/WeatherApi?city=%E5%8C%97%E4%BA%AC";  
    $xml = simplexml_load_file("compress.zlib://".$url);  
    $json=json_encode($xml);  
    $arr=json_decode($json,true);  
    print_r($arr);  

【二】Curl模拟post请求

有些时候我们会发现file_get_contents()或者simplexml_load_file()不好用了

这是因为对方做了一些防范措施


这里我们就需要Curl模拟post请求

    header('content-type:text/html;charset=utf-8');  
    function curlPost($url,$data,$method){  
        $ch = curl_init();   //1.初始化  
        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
        //4.参数如下  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器  
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
            curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容  
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
          
        if($method=="POST"){//5.post方式的时候添加数据  
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
        }  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        $tmpInfo = curl_exec($ch);//6.执行  
      
        if (curl_errno($ch)) {//7.如果出错  
            return curl_error($ch);  
        }  
        curl_close($ch);//8.关闭  
        return $tmpInfo;  
    }  
    $data=array('name' => '1234');  
    $url="http://www.sohu.com/";  
      
    $method="GET";  
    $file=curlPost($url,$data,$method);  
    $file=mb_convert_encoding($file,'UTF-8','GBK');  
    echo $file;  




当然还有的需要cookie认证登陆

我们可以这样

    <?php  
        $cookie_file = tempnam('./temp','cookie');  
        function weixinPost($url,$data,$method,$setcooke=false,$cookie_file=false){  
            $ch = curl_init();   //1.初始化  
            curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
            //4.参数如下      
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');  //模拟浏览器  
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
            curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
              
            if($method=="POST"){//5.post方式的时候添加数据     
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
            }  
            if($setcooke==true){  
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  
            }else{  
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);  
            }  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
            $tmpInfo = curl_exec($ch);//6.执行  
      
            if (curl_errno($ch)) {//7.如果出错  
                return curl_error($ch);  
            }  
            curl_close($ch);//8.关闭  
            return $tmpInfo;  
        }  
        $data=array('username' => '***','password'=>'***');  
        $url="http://www.xinxinj.com/login.php";  
        $method="POST";  
        $file=weixinPost($url,$data,$method,true,$cookie_file);  
        echo $file;  
              
        $url="http://www.xinxinj.com/admin.php";  
        $method="GET";  
        $file=weixinPost($url,$data,$method,false,$cookie_file);  
        echo $file;  
              
    ?>  

结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值