网络爬虫之模拟登陆

这几天看了一点不带验证码的模拟登陆的东西,理解的不深,但还是写一点点总结吧

  • 先说说我的理解

    • 所谓模拟登陆就是通过代码将浏览器访问一个网站的过程模拟出来
    • 在我们使用的浏览器上面保留着一个叫做cookies表单的东西
    • 这表单记录着我们日常访问网站时的一些信息,比如说我们的账号和密码,在登录的时候有记住密码功能,记住的密码就是保留在这个cookies表单中的
    • 他还保留着一部分我们访问的这个网站的一些喜好信息,这样这个网站就可以根据这个cookies表单数据来给我们推送我们喜欢的内容了
    • 那么我们平时登陆一个网站的时候先填入密码,然后浏览器将我们的密码放在这个cookies表单中,再拿着这个cookies数据去访问服务器,如果我们的账号和密码通过了服务器的验证,服务器就会给我们返回我们需要的网站数据
    • 模拟登陆就是直接将(包含账号很密码的)cookies数据拿着通过http去访问url,如果服务器验证成功,就会返回给我们登陆后的页面数据
  • 至于说那个cookies表单,则需要通过网络爬虫去获取到

  • 我用的是Fiddler这个爬虫,配置爬虫和浏览器关联这个大家自行去百度
  • 关联之后,通过浏览器登录网站,在爬虫上就会留下每一个访问网络的记录,找到登陆的那次访问记录,点击,在爬虫的右边勾选Inspectors -> WebForms 在这里就可以看到这个cookies表单了
  • 我要模拟登陆的人人网,现在上代码
package no_01;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;


public class Test222 {
    /*输入你的用户名及密码 ,这里输入你的账号和密码*/
   private static String userName = "";  
   private static String password = "";  
    //这是登陆的时候拿着cookies表单需要访问的url
   private static String renRenLoginURL = "http://www.renren.com/PLogin.do";  
   private HttpResponse response;  
   private HttpClient httpclient = new DefaultHttpClient();  

   private boolean login() {  
      //封装登陆访问的url
       HttpPost httpost = new HttpPost(renRenLoginURL);  
       // All the parameters post to the web site
       /*封装cookies数据*/
      //建立一个NameValuePair数组,用于存储欲传送的cookies参数
       List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
       nvps.add(new BasicNameValuePair("domain", "renren.com"));   
       nvps.add(new BasicNameValuePair("email", userName));
       nvps.add(new BasicNameValuePair("password", password));  
       try {  
           /*登陆成功,获取返回的数据,即html文件*/
           httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));  
           response = httpclient.execute(httpost);  
       } catch (Exception e) {  
           e.printStackTrace();  
           return false;  
       } finally {  
           httpost.abort();  
       }  
       return true;  
   }  

   private String getRedirectLocation() { 
       /*获取响应的头 url*/ 
       Header locationHeader = response.getFirstHeader("Location");  
       if (locationHeader == null) {  
           return null;
       }  
       return locationHeader.getValue();  
   }  
    /*获取html文本*/ 
   private String getText(String redirectLocation) {  
       HttpGet httpget = new HttpGet(redirectLocation);  
       // Create a response handler  
       ResponseHandler<String> responseHandler = new BasicResponseHandler();  
       String responseBody = "";  
       try {  
           responseBody = httpclient.execute(httpget, responseHandler);  
       } catch (Exception e) {  
           e.printStackTrace();  
           responseBody = null;
       } finally { 
           httpget.abort();
           httpclient.getConnectionManager().shutdown();
       }  
       return responseBody;  
   }  

   public void printText() {
       /*如果注册成功了,输入相应后的html*/
       if (login()) {  
           String redirectLocation = getRedirectLocation();  
           if (redirectLocation != null) { 
               System.out.println(getText(redirectLocation));  
           }  
       }  
   } 
    public static void main(String[] args) {
        Test222 a = new Test222();
        a.printText();
    } 
}

大概就是这些了,我也是刚看,好多都不知道,只是大概写一下自己的认知,如果有错误欢迎大家在评论区指出

猜你喜欢

转载自blog.csdn.net/asffghfgfghfg1556/article/details/79319106