zoukankan      html  css  js  c++  java
  • curl模拟自动登陆&采集网页数据

    <!DOCTYPE>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>模拟登录测试</title>
    </head>
    
    <body>
    <?php
        //curl模拟自动登陆认证 
        $url = ""; 
        $User_Agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";    
        $post_data = "muser=***&passwd=***";    
        $refer="http://***/";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);        //设置URL
        curl_setopt($ch, CURLOPT_HEADER, true);        //设置显示响应头
        curl_setopt($ch,CURLOPT_USERAGENT,$User_Agent); //设置代理浏览器
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT,5);//超时处理
        curl_setopt($ch,CURLOPT_REFERER,$refer);    //设置来源网站
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//设置重定向
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//设置参数
        $content = curl_exec($ch);
    
        $headArr = explode("
    ", $content);        //解析url
        foreach ($headArr as $loop) {
            //echo htmlspecialchars($loop)."<br />";
          if(strpos($loop, "http://***/default.aspx?id=") !== false){//找到Location
                $id = trim(substr($loop, 46));
          }
          if(strpos($loop, "Set-Cookie") !== false){//找到Cookie
                $cookie = trim(substr($loop, 12));
          }
        }
        $rurl="****?id=".$id;
        echo "edengUrl:<br>".$rurl;
        echo "<br />";
        echo "setcookie:<br>".$cookie."<br />";
        curl_close($ch);
        
        
        //结果处理
        $url = $rurl; 
        $User_Agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";    
        $refer="http://****/";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);        //设置URL
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);        //设置cookies
        curl_setopt($ch,CURLOPT_USERAGENT,$User_Agent); //设置代理浏览器
        curl_setopt($ch, CURLOPT_TIMEOUT,5);//超时处理
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch,CURLOPT_REFERER,$refer);    //设置来源网站
        $content = curl_exec($ch);
        
        //解析html
        echo "<br />";
        echo "<br />解析:";
        echo "<br />";
        $content=strip_tags($content);//去除html标签
        echo $content;
        echo "<br />";
        echo "<br />";
        $sk=strpos(trim($content), "当前用户")+28;
        $name = trim(substr($content,$sk,9));
        $arr=explode("    ",$name);
        echo $sk."<br />".$arr[0]."<br />";
            
        curl_close($ch); 
        
    ?>
    </body>
    </html>
  • 相关阅读:
    python中map()函数
    Numpy学习—np.random.randn()、np.random.rand()和np.random.randint()
    列表、集合和字典推导式
    pandas iloc函数
    python -- 类中self到底有什么用?再续
    python apply()函数
    python 中关于self到底有什么用续
    python——类中的self到底有什么作用
    类初始化的参数可以是任何形式
    python高级(元类、自定义元类)
  • 原文地址:https://www.cnblogs.com/zeze/p/5079290.html
Copyright © 2011-2022 走看看