仿人人网java_Java爬虫——人人网模拟登录

人人网登录地址:http://www.renren.com/

此处登录没有考虑验证码验证码。

e52d307e35b0f27a59862933317093f4.png

首先对登录方法进行分析

有两种方法。

一)在Elements中分析源码

发现登录点击后的事件是http://www.renren.com/PLogin.do

9274a1782d794f2e809c923c02c0bec0.png

二)在Network中分析网络请求

请求链接:http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2017110237292

e3fee9314e15847b81216d20cbb753aa.png

表单数据 :

email 账号用户名

icode 验证码,可为空

origURL : http://www.renren.com/home

domain:renren.com

key_id:1

captcha_type:web_login

password: 密码,需要对输入的密码进行加密处理

rkey: 密码处理

f: 未知

此处采取直接使用Elements发现的触发事件。

1 package 人人网模拟登录;

2

3 import org.apache.http.Header;

4 import org.apache.http.NameValuePair;

5 import org.apache.http.client.ResponseHandler;

6 import org.apache.http.client.entity.UrlEncodedFormEntity;

7 import org.apache.http.client.methods.CloseableHttpResponse;

8 import org.apache.http.client.methods.HttpGet;

9 import org.apache.http.client.methods.HttpPost;

10 import org.apache.http.impl.client.BasicResponseHandler;

11 import org.apache.http.impl.client.CloseableHttpClient;

12 import org.apache.http.impl.client.HttpClients;

13 import org.apache.http.message.BasicNameValuePair;

14 import java.util.ArrayList;

15 import java.util.List;

16

17 public class Renren {

18 public static void main(String[] args) throws Exception{

19 CloseableHttpClient closeableHttpClient = HttpClients.createDefault() ;

20 HttpPost httpPost = new HttpPost("http://www.renren.com/PLogin.do") ;

21

22 String userName = " " ; // 账号写入

23 String passWord = " " ; // 密码写入

24 List dlbd = new ArrayList();

25 // 登录表单设置

26 dlbd.add(new BasicNameValuePair("domain", "renren.com"));

27 dlbd.add(new BasicNameValuePair("isplogin", "true"));

28 dlbd.add(new BasicNameValuePair("submit", "登录"));

29 dlbd.add(new BasicNameValuePair("email", userName));

30 dlbd.add(new BasicNameValuePair("password", passWord));

31 httpPost.setEntity(new UrlEncodedFormEntity(dlbd));

32 // Post请求

33 CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost) ;

34 // 获取响应头

35 Header locationHeader = closeableHttpResponse.getFirstHeader("Location");

36 // Get请求

37 String header = locationHeader.getValue();

38 HttpGet httpGet = new HttpGet(header) ;

39 ResponseHandler responseHandler = new BasicResponseHandler();

40 String responseBody = closeableHttpClient.execute(httpGet, responseHandler);

41 System.out.println(responseBody);

42 }

43 }

f4580d6d0c446c90cc57891dd030c0bf.png

登录成功

如果之前在网页登录失败次数过多,可能会导致爬虫模拟登录需要验证码,而此处是考虑不需要验证码的情况,所以可能会登录失败,解决方法可以是清理本机Cookie。

作者:AntzUhl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值