Rumah >pembangunan bahagian belakang >tutorial php >PHP中CURL技术模拟登陆抓取网站信息,用与微信公众平台成绩查询_PHP教程

PHP中CURL技术模拟登陆抓取网站信息,用与微信公众平台成绩查询_PHP教程

WBOY
WBOYasal
2016-07-13 10:36:071252semak imbas

伴随微信的红火,微信公众平台成为许多开发者的下一个目标。笔者本身对于这种新鲜事物没有如此多的吸引力。但是最近有朋友帮忙开发微信公众平台中一个成绩查询的功能。于是便在空余时间研究了一番。

  主要的实现步骤是,通过PHP的CURL技术模拟登陆目标网站,通过登陆的用户,获取到用户的成绩信息,使用正则表达式对数据进行抓取和存储,使用HTML技术对数据进行重新弄排版。

  微信公众平台的功能就是通过浏览的目的来实现成绩查询。整体的技术实现就在于PHP的CURL技术。下面就随便找了一个文件,获取成绩。具体代码如下。

  


<span <</span><span HTML</span><span ></span>
<span <</span><span HEAD</span><span ><</span><span TITLE</span><span ></span>请您登录<span </</span><span TITLE</span><span ></span>
<span <</span><span script </span><span language</span><span ="JavaScript"</span><span ></span>
<span function</span><span  Judge()
    {
    </span><span var</span><span  WebUserNO</span><span =</span><span document.all[</span><span "</span><span WebUserNO</span><span "</span><span ].value;
    </span><span if</span><span (WebUserNO</span><span ==</span><span ""</span><span )
       {alert(</span><span "</span><span 登录用户不能为空!</span><span "</span><span );
       document.all[</span><span "</span><span WebUserNO</span><span "</span><span ] .focus();
       </span><span return</span> <span false</span><span ;
       }
    
}
</span><span </</span><span script</span><span ></span>
<span <</span><span META </span><span http-equiv</span><span =Content-Type </span><span content</span><span ="text/html; charset=gb2312"</span><span ></span>
<span <</span><span STYLE </span><span type</span><span =text/css</span><span ></span><span TD </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  12px
</span><span }</span><span 
p1 </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  12px
</span><span }</span><span 
INPUT </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  12px
</span><span }</span><span 
p2 </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  12px</span><span ;</span><span  LINE-HEIGHT</span><span :</span><span  14pt
</span><span }</span><span 
p3 </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  14px
</span><span }</span><span 
p4 </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  14px</span><span ;</span><span  LINE-HEIGHT</span><span :</span><span  14pt
</span><span }</span><span 
p5 </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  16px
</span><span }</span><span 
p6 </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  14px</span><span ;</span><span  LINE-HEIGHT</span><span :</span><span  180%
</span><span }</span><span 
p7 </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  12px</span><span ;</span><span  COLOR</span><span :</span><span  #136792</span><span ;</span><span  LINE-HEIGHT</span><span :</span><span  160%
</span><span }</span><span 
BIG </span><span {</span><span 
    FONT-SIZE</span><span :</span><span  18px
</span><span }</span><span 
A:link </span><span {</span><span 
    COLOR</span><span :</span><span  #0000ff
</span><span }</span><span 
A:visited </span><span {</span><span 
    COLOR</span><span :</span><span  #0000ff
</span><span }</span><span 
A:hover </span><span {</span><span 
    COLOR</span><span :</span><span  #ff0000
</span><span }</span><span 
hand </span><span {</span><span 
    CURSOR</span><span :</span><span  hand</span><span ;</span><span  BACKGROUND-COLOR</span><span :</span><span  rgb(208,207,192)
</span><span }</span>
<span </</span><span STYLE</span><span ></span>
<span <!--</span><span style end</span><span --></span>
<span <</span><span META </span><span content</span><span ="MSHTML 6.00.2600.0"</span><span  name</span><span =GENERATOR</span><span ></</span><span HEAD</span><span ></span>
<span <</span><span BODY </span><span bgColor</span><span =#ffffff </span><span topMargin</span><span =7 </span><span marginheight</span><span ="0"</span><span  marginwidth</span><span ="25"</span><span ></span>
<span <</span><span form </span><span name</span><span ="LoginForm"</span><span  method</span><span ="post"</span><span  action</span><span ="qing.php"</span><span ></span>
<span <</span><span TABLE </span><span cellSpacing</span><span =0 </span><span cellPadding</span><span =0 </span><span width</span><span ="100%"</span><span  border</span><span =0</span><span ></span>
  <span <</span><span TBODY</span><span ></span>
  <span <</span><span TR</span><span ></span>
    <span <</span><span TD </span><span bgColor</span><span =#e6e6e6 </span><span height</span><span =20</span><span ></</span><span TD</span><span ></span>
      <span <</span><span TD </span><span align</span><span =right </span><span bgColor</span><span =#e6e6e6</span><span ></span><span  </span><span </</span><span TD</span><span ></span>
    <span </</span><span TR</span><span ></</span><span TBODY</span><span ></</span><span TABLE</span><span ><</span><span BR</span><span ></span>
<span <</span><span TABLE </span><span cellSpacing</span><span =0 </span><span cellPadding</span><span =1 </span><span width</span><span =492 </span><span align</span><span =center </span><span border</span><span =0</span><span ></span>
  <span <</span><span TBODY</span><span ></span>
  <span <</span><span TR</span><span ></span>
    <span <</span><span TD</span><span ></span>
      <span <</span><span TABLE </span><span borderColor</span><span =#c1eaff </span><span cellSpacing</span><span =0 </span><span cellPadding</span><span =20 </span><span width</span><span =474 
      </span><span align</span><span =center </span><span border</span><span =1</span><span ></span>
        <span <</span><span TBODY</span><span ></span>
        <span <</span><span TR</span><span ></span>
          <span <</span><span TD</span><span ><</span><span TABLE </span><span width</span><span =283 </span><span height</span><span ="100"</span><span  
              border</span><span =0 </span><span align</span><span =center </span><span cellPadding</span><span =0 </span><span cellSpacing</span><span =0</span><span ></span>
                                   <span <</span><span tr</span><span ></span>
                      <span <</span><span td </span><span width</span><span ="50"</span><span  rowspan</span><span ="4"</span><span ></span><span  </span><span </</span><span td</span><span ></span>
                    <span <</span><span td </span><span align</span><span ="left"</span><span ></span>
                      <span </</span><span td</span><span ></span>
                      <span </</span><span tr</span><span ></span>
                                   <span <</span><span tr</span><span ></span>
                                     <span <</span><span td </span><span height</span><span ="22"</span><span  align</span><span ="left"</span><span ></span><span 用户名:
                                     </span><span <</span><span input </span><span name</span><span ="WebUserNO"</span><span  type</span><span ="text"</span><span  id</span><span ="WebUserNO"</span><span  size</span><span ="12"</span><span ></</span><span td</span><span ></span>
                                   <span </</span><span tr</span><span ></span>
                                   <span <</span><span tr</span><span ></span>
                                     <span <</span><span td </span><span height</span><span ="22"</span><span  align</span><span ="left"</span><span ></span>密<span   </span><span 码:
                                     </span><span <</span><span input </span><span name</span><span ="Password"</span><span  type</span><span ="password"</span><span  id</span><span ="Password"</span><span  size</span><span ="12"</span><span ></</span><span td</span><span ></span>
                                   <span </</span><span tr</span><span ></span>
                                   <span <</span><span tr</span><span ></span>
                                     <span <</span><span td </span><span height</span><span ="22"</span><span  align</span><span ="left"</span><span  valign</span><span ="middle"</span><span ><</span><span p</span><span ></span><span 附加码:
                                     </span><span <</span><span input </span><span name</span><span ="Agnomen"</span><span  type</span><span ="text"</span><span  id</span><span ="Agnomen"</span><span  size</span><span ="12"</span><span ></span>
                                     <span </</span><span p</span><span ></span>
                                     <span <</span><span p</span><span ><</span><span A </span><span href</span><span ="User_JSP/FuJiaMa.htm"</span><span  target</span><span ="_blank"</span> <span ><</span><span img </span><span src</span><span ="http://218.61.108.163/ACTIONVALIDATERANDOMPICTURE.APPPROCESS"</span><span  width</span><span ="60"</span><span  height</span><span ="20"</span><span  alt</span><span ="验证码说明"</span><span  border</span><span ="0"</span><span ></</span><span a</span><span ></</span><span p</span><span ></</span><span td</span><span ></span>
                                   <span </</span><span tr</span><span ></span>
                  <span <</span><span tr </span><span align</span><span ="center"</span><span ></span> 
                    <span <</span><span td </span><span colspan</span><span ="2"</span><span ><</span><span input </span><span type</span><span ="image"</span><span  border</span><span ="0"</span><span  name</span><span ="submit"</span><span  src</span><span ="http://218.61.108.163/User_JSP/images/Logon.gif"</span><span  width</span><span ="37"</span><span  height</span><span ="18"</span><span  onClick</span><span ="javascript:return Judge();"</span><span ></span>
                    
                    <span </</span><span td</span><span ></span>
                <span </</span><span tr</span><span ></span>    
                  <span <</span><span tr</span><span ></span> 
                    <span <</span><span td </span><span colspan</span><span ="2"</span><span ><</span><span div </span><span align</span><span ="center"</span><span ><</span><span input </span><span name</span><span ="applicant"</span><span  type</span><span ="hidden"</span><span  value</span><span ="ACTIONQUERYSTUDENTSCORE"</span><span ></</span><span div</span><span ></</span><span td</span><span ></span>
                <span </</span><span tr</span><span ></span>    
                <span </</span><span TABLE</span><span ></span>
            <span <</span><span br</span><span ></span>
          <span </</span><span TD</span><span ></span>
        <span </</span><span TR</span><span ></span>
        <span </</span><span TABLE</span><span ></</span><span TD</span><span ></</span><span TR</span><span ></</span><span TBODY</span><span ></</span><span TABLE</span><span ></span>
<span </</span><span form</span><span ></span>        
<span <</span><span BR</span><span ></span>
<span <</span><span BR</span><span ></span>
<span </</span><span BODY</span><span ></</span><span HTML</span><span ></span>


qing.php

<?<span php
    </span><span $cookie_file</span> = <span tempnam</span>('./temp','cookie'<span );
    </span><span $login_url</span> = 'http://218.61.108.163/ACTIONQUERYSTUDENTSCORE.APPPROCESS'<span ;
    
    </span><span $post_fields</span> = 'WebUserNO=stuid&Password=passwd&Agnomen=code&applicant=ACTIONQUERYGRADUATESCHOOLREPORTBYSELF'<span ;
    
    </span><span $ch</span> = curl_init(<span $login_url</span><span );
    curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span );
    curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, 1<span );
    curl_setopt(</span><span $ch</span>, CURLOPT_POST, 1<span );
    curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEJAR, <span $cookie_file</span><span );
    curl_setopt(</span><span $ch</span>, CURLOPT_POSTFIELDS, <span $post_fields</span><span );
    curl_exec(</span><span $ch</span><span );
    curl_close(</span><span $ch</span><span );
    
    </span><span $url</span>='http://218.61.108.163/ACTIONQUERYGRADUATESCHOOLREPORTBYSELF.APPPROCESS'<span ;
    </span><span $ch</span> = curl_init(<span $url</span><span );
    curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span );
    curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, 0<span );
    curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEFILE, <span $cookie_file</span><span );
    </span><span $contents</span> = curl_exec(<span $ch</span><span );
    
    </span><span //</span><span 正则表达式提取数据。</span>
    <span $match</span>="|(<=<td>).*(?=</td>)|"<span ;
    </span><span preg_match_all</span>(<span $match</span>,<span $contents</span>,<span $b</span><span );
    </span><span $abc</span> = <span $b</span>[0<span ];
    </span><span $abs</span> = <span $b</span>[1<span ];
    </span><span $abd</span> = <span $b</span>[1<span ];
    </span><span echo</span> <span $abc</span><span ;
    </span><span echo</span> <span $abs</span><span ;
    </span><span echo</span> <span $abd</span><span ;
    curl_close(</span><span $ch</span><span );
</span>?>


获取到成绩的界面

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/741659.htmlTechArticle伴随微信的红火,微信公众平台成为许多开发者的下一个目标。笔者本身对于这种新鲜事物没有如此多的吸引力。但是最近有朋友帮忙开发...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn