//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. VM不能ping通www.baidu.com的问题【NAT】 更新了【桥接模式】

    NAT模式 1.为NAT设置子网及掩码等信息 点击VMWare菜单栏“编辑” —> 虚拟网络编辑器 [下图第一张图]—> 选中VMnet8(当然这个看自己的设备后缀是多少了,只要外部连接方 ...

  2. POJ 3469(Dual Core CPU-最小割)[Template:网络流dinic V2]

    Language: Default Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 19321 ...

  3. webpack打包问题

    最近项目里需要替换一个logo,原先的logo打包后生成的静态文件里有对应的图片,替换了新的的图片打包后并没有生成相应的静态文件,两个图片都在同一个文件目录下,而且图片是直接引入并不会出现打包不到图片 ...

  4. [未完结]数字微分分析法的直线绘制(DDA)

    注意! 本文被第1次更新,可能存在后续更新 直线画法 直线的斜截式方程 在二维空间下,一条直线的方程可以被描述为若干种形式,其中比较常见的一种是斜截式方程: \[y=kx+b\] 其中\(k\)称为直 ...

  5. jQuery UI Autocomplete是jQuery UI的自动完成组件

    支持的数据源 jQuery UI Autocomplete主要支持字符串Array.JSON两种数据格式. 普通的Array格式没有什么特殊的,如下: ? 1 ["cnblogs" ...

  6. JavaScript事件在WebKit中的处理流程研究

    本文主要探讨了JavaScript事件在WebKit中的注冊和触发机制. JS事件有两种注冊方式: 通过DOM节点的属性加入或者通过node.addEventListener()函数注冊: 通过DOM ...

  7. SSH实现在WIN7系统下访问虚拟机中的Linux系统

    使用的是centos6.4进行练习的,安装的是vmware8虚拟机.以下是总结的一些步骤: 一.确保vmware使用NAT的连接方式,如做地址.端口映射 首先查看vmware的中网络连接的一些方式:E ...

  8. jquery 效果网址分享

     http://www.lanrentuku.com/js/ http://www.baidu.com/link?url=2nuImAliKGCKyDeJ7ln2DR_2if5uKgr-em6a3dx ...

  9. vmware安装操作系统

    安装的时候,直接选择“从光盘或者映像安装”就可以了,很方便的.不需要再去从u盘什么的安装了.

  10. 使用Primose方式解决异步编程回调的一些问题--animate动画的例子

    function animate(dis, time) { var def = $.Deferred(); $('.boll') .animate({ left: dis + 'px' }, time ...