Java爬虫——人人网模拟登录
人人网登录地址:http://www.renren.com/
此处登录没有考虑验证码验证码。
首先对登录方法进行分析
有两种方法。
一)在Elements中分析源码
发现登录点击后的事件是http://www.renren.com/PLogin.do
二)在Network中分析网络请求
请求链接:http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2017110237292
表单数据 :
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<NameValuePair> dlbd = new ArrayList<NameValuePair>();
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<String> responseHandler = new BasicResponseHandler();
40 String responseBody = closeableHttpClient.execute(httpGet, responseHandler);
41 System.out.println(responseBody);
42 }
43 }
登录成功
如果之前在网页登录失败次数过多,可能会导致爬虫模拟登录需要验证码,而此处是考虑不需要验证码的情况,所以可能会登录失败,解决方法可以是清理本机Cookie。
作者: AntzUhl
首发地址博客园:http://www.cnblogs.com/LexMoon/
代码均可在Github上找到(求Star) : Github
个人博客 : http://antzuhl.cn/
公众号 |
![]() |
赞助
支付宝 |
微信 |
![]() |
![]() |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 接口重试的7种常用方案!