//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:实现有验证码的模拟登陆的更多相关文章

  1. 爬虫之 cookie , 验证码,模拟登陆,线程

    需求文档的定制 糗事百科的段子内容和作者(xpath的管道符)名称进行爬取,然后存储到mysql中or文本 http://sc.chinaz.com/jianli/free.html爬取简历模板 HT ...

  2. Python爬虫实战点触验证码, 模拟登陆bilibili

    爬虫思路如下: 利用自动化爬虫工具 Selenium 模拟点击输入等操作来进行登录 分析页面,获取点触验证码的点触图片,通过将图片发送给超级鹰打码平台识别后获取坐标信息 根据超级鹰返回的数据,模拟坐标 ...

  3. python+requests模拟登陆 学校选课系统

    最近学校让我们选课,每天都有不同的课需要选....然后突发奇想试试用python爬学校选课系统的课程信息 先把自己的浏览器缓存清空,然后在登陆界面按f12 如图: 可以看到登陆时候是需要验证码的,验证 ...

  4. HttpClient+Jsoup模拟登陆贺州学院教务系统,获取学生个人信息

    前言 注:可能学校的教务系统已经做了升级,当前的程序不知道还能不能成功获取信息,加上已经毕业,我的账户已经被注销,试不了,在这里做下思路跟过程的记录. 在我的毕业设计中”基于SSM框架贺州学院校园二手 ...

  5. 使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)

    urilib是python的标准库,当我们使用Python爬取网页数据时,往往用的是urllib模块,通过调用urllib模块的urlopen(url)方法返回网页对象,并使用read()方法获得ur ...

  6. 第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别

    第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别 第一步.首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://gith ...

  7. c# 使用 HttpWebRequest模拟登陆(附带验证码)

    在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...

  8. 用session模拟登陆,手动输入验证码

    # 本练习是模拟登陆及验证码处理(把验证码下载到本地后手动输入) # 1 通过分析页面获得form表单的登陆接口为 action="https://www.douban.com/accoun ...

  9. 验证码破解 | Selenium模拟登陆微博

    模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...

随机推荐

  1. Ubuntu 16.04下配置Golang开发环境

    安装之前先要明白两个变量,后面介绍安装时,会用这两个变量 GOROOT   , 这是go的工作目录,比如 /home/[替换为你的用户名]/go/work GOPATH    , 这是go的安装目录, ...

  2. mysql字符太长警告

    用navicateclient,打开相应的数据库. 打开函数.找相应的val()函数,进行编辑,就能够!编辑范围为4000

  3. FPGA机器学习之机器学习的n中算法总结1

    机器学习是AI领域的重要一门学科.前面我描写叙述过.我计划从事的方向是视觉相关的机器学习分类识别,所以可能在每一个算法的分析中,仅仅增加在视频.视觉领域的作用. 我毛华望QQ849886241.技术博 ...

  4. vue-导入静态文件

    vue导入静态文件不用像网上说的那么麻烦,其实跟普通Django项目导入类似,vue项目中有一个static文件,将你的静态文件放入到里面,然后引入就好 导入的时候和普通Django程序类似:↓ &l ...

  5. 热烈庆祝UE4完全免费Free---GitHub的关联方式

    热烈庆祝UE4完全免费Free---GitHub的关联方式 时间:2015-03-03 18:38:52      阅读:3007      评论:0      收藏:0      [点我收藏+] 标 ...

  6. iOS移动开发周报-第17期

    lhq iOS移动开发周报-第17期 前言 欢迎国内的iOS同行或技术作者向我提交周报线索,线索可以是新闻.教程.开发工具或开源项目,将相关文章的简介和链接在微博上发布并 @唐巧_boy 即可. [摘 ...

  7. Android组件系列----ContentProvider内容提供者【4】

    (4)单元測试类: 这里须要涉及到另外一个知识:ContentResolver内容訪问者. 要想訪问ContentProvider.则必须使用ContentResolver. 能够通过ContentR ...

  8. 【BZOJ1520】[POI2006]Szk-Schools KM算法

    [BZOJ1520][POI2006]Szk-Schools Description Input Output 如果有可行解, 输出最小代价,否则输出NIE. Sample Input 5 1 1 2 ...

  9. 按照eslint 规范写代码 [eslint] 'flag' is assigned to itself. (no-self-assign)

    按照eslint 规范写代码 [eslint] 'flag' is assigned to itself. (no-self-assign)

  10. Java Virtual Machine (JVM) objects 虚拟机实例的产生 退出 两种线程

    Apache Spark is built around a distributed collection of immutable Java Virtual Machine (JVM) object ...