Java爬虫——人人网模拟登录

简介: 人人网登录地址:http://www.renren.com/ 此处登录没有考虑验证码验证码。 首先对登录方法进行分析 有两种方法。 一)在Elements中分析源码   发现登录点击后的事件是http://www.

人人网登录地址: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。

目录
相关文章
|
5月前
|
数据采集 网络协议 Java
Java爬虫框架下代理使用中的TCP连接池问题及解决方案
Java爬虫框架下代理使用中的TCP连接池问题及解决方案
|
6月前
|
数据采集 Java API
Java爬虫实战:API商品数据接口调用
随着互联网的发展,越来越多的商家开始将自己的商品数据通过API接口对外开放,以供其他开发者使用。这些API接口可以提供丰富的商品数据,包括商品名称、价格、库存、图片等信息。对于Java爬虫开发者来说,通过调用这些API接口,可以更加便捷地获取商品数据,避免了爬取网页数据的繁琐过程。本文将介绍如何使用Java调用API商品数据接口,实现商品数据的获取和处理。
|
1月前
|
数据采集 前端开发 JavaScript
Java网络爬虫实践:解析微信公众号页面的技巧
Java网络爬虫实践:解析微信公众号页面的技巧
|
1月前
|
数据采集 Web App开发 Java
盘点Java爬虫框架
盘点Java爬虫框架
35 0
|
3月前
|
数据采集 存储 Java
Java爬虫与SSL代理:实际案例分析与技术探讨
Java爬虫与SSL代理:实际案例分析与技术探讨
|
4月前
|
数据采集 Java API
百度搜索:蓝易云【Java爬虫与Python爬虫有什么区别】
综上所述,Java爬虫和Python爬虫在语言特性、代码复杂性、生态系统、并发处理和执行性能等方面存在一些区别。选择使用哪种爬虫工具取决于具体的需求、项目要求和个人技术偏好。
45 0
|
4月前
|
数据采集 JavaScript 前端开发
Java爬虫攻略:应对JavaScript登录表单
Java爬虫攻略:应对JavaScript登录表单
|
9月前
|
数据采集 Java
java随机姓名 根据网络爬虫爬取百家姓和名字
java随机姓名 根据网络爬虫爬取百家姓和名字
92 0
|
13天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
17天前
|
数据采集 Python
【python】爬虫-西安医学院-校长信箱
本文以西安医学院-校长信箱为基础来展示爬虫案例。来介绍python爬虫。
【python】爬虫-西安医学院-校长信箱