httpclient:实现有验证码的模拟登陆
//1、这种方式是先把验证码的图片下载到本地、并且根据网页解析获得token值
//2、手动在控制台输入验证码
//3、因为验证码图片已经下载下来,后面就可以使用图像文字识别
package DoubanSimulate; import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; public class DoubanSimulate {
private static HttpClient httpClient = new DefaultHttpClient(); // 登陆入口
public static void loginDouban() {
String login_src = "https://accounts.douban.com/login";
// 输入用户名及密码
String form_email = "2692613726@qq.com";
String form_password = "0315209X";
String captcha_solution = "";
// 获取验证码
String captcha_id = getImgID();
String login = "登录";
// 输入验证码
System.out.println("请输入验证码:");
BufferedReader buff = new BufferedReader(new InputStreamReader(
System.in));
try {
captcha_solution = buff.readLine();
} catch (IOException e) {
e.printStackTrace();
} // 构造参数,即模拟需要输入的参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("form_email", form_email));
list.add(new BasicNameValuePair("form_password", form_password));
list.add(new BasicNameValuePair("captcha-solution", captcha_solution));
list.add(new BasicNameValuePair("captcha-id", captcha_id));
list.add(new BasicNameValuePair("login", login));
HttpPost httpPost = new HttpPost(login_src);
try {
// 向后台请求数据,登陆网站
httpPost.setEntity(new UrlEncodedFormEntity(list));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
} catch (Exception e) {
// TODO: handle exception
}
} // 获取验证码图片“token”
private static String getImgID() {
String src = "https://www.douban.com/accounts/login";
HttpGet httpGet = new HttpGet(src);
String url = "";
String token="";
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity, "utf-8");
Document doc = Jsoup.parse(content);
url = doc.select("div[class=item item-captcha]").select("div")
.select("img[id=captcha_image]").attr("src");
int start = url.indexOf("=");
int end = url.indexOf("&");
token = url.substring(start+1, end);
// 下载验证码并存储到本地
downImg(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//https://www.douban.com/misc/captcha?id=f7enPStQocewglTaSeFdgfTu:en&size=s
return token;
} private static void downImg(String src) throws IOException {
File fileDir = new File("D:\\IdentifyingCode");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
// 图片下载保存地址
File file = new File("D:\\IdentifyingCode\\yzm.png");
if (file.exists()) {
file.delete();
}
InputStream input = null;
FileOutputStream out = null;
HttpGet httpGet = new HttpGet(src);
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
input = entity.getContent();
int i = -1;
byte[] byt = new byte[1024];
out = new FileOutputStream(file);
while ((i = input.read(byt)) != -1) {
out.write(byt);
}
System.out.println("图片下载成功!");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
out.close(); } //登陆后,便可以输入一个或者多个url,进行请求
public static String gethtml(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;
}
//一个实例,只请求了一个url
public static void main(String[] args) throws ClientProtocolException, IOException {
loginDouban();
String redir="https://www.douban.com/people/conanemily/contacts";
// System.out.println(gethtml(redir));
String cc=gethtml(redir);
System.out.println(cc); } }
httpclient:实现有验证码的模拟登陆的更多相关文章
- 爬虫之 cookie , 验证码,模拟登陆,线程
需求文档的定制 糗事百科的段子内容和作者(xpath的管道符)名称进行爬取,然后存储到mysql中or文本 http://sc.chinaz.com/jianli/free.html爬取简历模板 HT ...
- Python爬虫实战点触验证码, 模拟登陆bilibili
爬虫思路如下: 利用自动化爬虫工具 Selenium 模拟点击输入等操作来进行登录 分析页面,获取点触验证码的点触图片,通过将图片发送给超级鹰打码平台识别后获取坐标信息 根据超级鹰返回的数据,模拟坐标 ...
- python+requests模拟登陆 学校选课系统
最近学校让我们选课,每天都有不同的课需要选....然后突发奇想试试用python爬学校选课系统的课程信息 先把自己的浏览器缓存清空,然后在登陆界面按f12 如图: 可以看到登陆时候是需要验证码的,验证 ...
- HttpClient+Jsoup模拟登陆贺州学院教务系统,获取学生个人信息
前言 注:可能学校的教务系统已经做了升级,当前的程序不知道还能不能成功获取信息,加上已经毕业,我的账户已经被注销,试不了,在这里做下思路跟过程的记录. 在我的毕业设计中”基于SSM框架贺州学院校园二手 ...
- 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)
urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...
- 第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别
第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别 第一步.首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://gith ...
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...
- 用session模拟登陆,手动输入验证码
# 本练习是模拟登陆及验证码处理(把验证码下载到本地后手动输入) # 1 通过分析页面获得form表单的登陆接口为 action="https://www.douban.com/accoun ...
- 验证码破解 | Selenium模拟登陆微博
模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...
随机推荐
- Android学习系列(二)布局管理器之线性布局的3种实现方式
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39643669 LinearLayout是Android控件中的线性布局控件,它包括的子控件 ...
- Use the command of tar to multi-part archive method.
We usually meet the package too large to upload internat space when upload have a limited .So we nee ...
- 《后会无期》票房赶超《小时代3》 大数据解读韩寒VS四娘之争
7月25日.韩寒导演的处女作<后会无期>零点首映,而郭四娘导演的<小时代3:刺金时代>比<后会无期>早上映一周.也就是7月17日正式公映,韩寒与四娘之间向来不缺乏话 ...
- ActiveMQ 消息持久化到Mysql数据库
[root@txylucky local]# tar -zxvf apache-activemq-5.15.8-bin.tar.gz[root@txylucky local]# mv apache-a ...
- Linux 系统编程中环境变量的使用
前言 在 UNIX Like 系统中,存有各类系统/应用程序的环境变量,可通过修改之改变系统/应用程序的执行效果:除此之外,用户还可以定义自己的环境变量,供自己写的程序使用.本文将说明如何在程序中设置 ...
- SQL创建触发器
更新: CREATE TRIGGER `r_users_1` AFTER UPDATE ON `users` FOR EACH ROW update `wxusers` set status=NEW. ...
- cmake中的变量和命令的大小写
1 cmake中要特别注意命令和变量的大小写 2 cmake的内置命令是不区分大小写的 3 cmake内置变量是区分大小写的,或者干脆就说,cmake的所有变量都是区分大小写的 这就是变量和命令的不同 ...
- 动态绑定允许我们在程序运行的过程中动态给class加上功能,这在静态语言中很难实现
https://www.liaoxuefeng.com/wiki/ # 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法, # 这就是动态语言的 ...
- Git 忽略上传文件设置 .gitignore exclude
1.项目根目录下创建.gitignore vi .gitignore #输入需要忽略的文件,支持正则表达式, *.o *.exe*.pyc 可以用git status来检查是否生效 未添加文件前: M ...
- Cocos2d-x动画播放(序列帧)
简介 Cocos2d-x中,动画的具体内容是依靠精灵显示出来的,为了显示动态图片,我们需要不停切换精灵显示的内容,通过把静态的精灵变为动画播放器从而实现动画效果.动画由帧组成,每一帧都是一个纹理,我们 ...