0
回答
htmunit模拟登录淘宝,拖拉验证码提示:验证失败请重试
开发十年,就只剩下这套Java开发体系了   

首先淘宝的拖拉验证码不是随时出现的 我提供一个帐号:qq1121212159

然后我尝试用javascript 在淘宝页面执行 

 var event = document.createEvent('MouseEvents');event.initMouseEvent('mousedown', true, true, document.defaultView,0,0,0,0,0, false, false, false, false, 11 ,null); nc_1_n1z.dispatchEvent(event);var event = document.createEvent('MouseEvents');event.initMouseEvent('mousemove', true, true, document.defaultView, 0,0,0, 290,290, false, false, false, false,0,null);nc_1_n1z.dispatchEvent(event);

提示验证成功 ,是OK的

当我通过htmunit的executeJavaScript方法去执行的时候 返回的页面提示:  哎呀,出错了,点击 刷新再来一次

具体代码如下:

package com.reptile.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.TextPage;
import com.gargoylesoftware.htmlunit.UnexpectedPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.html.HtmlVariable;
import com.gargoylesoftware.htmlunit.util.NameValuePair;

public class taobao {
private static String username="qq1121212159";


	private static CrawlerUtil crawlerUtil = new CrawlerUtil();
public static void main(String[] args) throws FailingHttpStatusCodeException, IOException, InterruptedException {
	Resttemplate resttemplate=new Resttemplate();
				Map<String,Object> map=new HashMap<String, Object>();
				Map<String,Object> data=new HashMap<String, Object>();
				boolean flg=false;
					int count=0;
			WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER);
			 webClient.getOptions().setUseInsecureSSL(true);
			 webClient.getCookieManager().setCookiesEnabled(true);// 开启cookie管理
//			 webClient.getOptions().setTimeout(100000);
			 webClient.getOptions().setCssEnabled(true);
			 webClient.getOptions().setJavaScriptEnabled(true);
//			 webClient.setJavaScriptTimeout(100000); 
			 webClient.getOptions().setRedirectEnabled(true);
			 webClient.getOptions().setThrowExceptionOnScriptError(false);
			 webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);
			 webClient.setAjaxController(new NicelyResynchronizingAjaxController());
			 WebRequest webRequests=new  WebRequest(new java.net.URL("https://login.taobao.com/member/request_nick_check.do?_input_charset=utf-8"));
		
			 List<NameValuePair> lists=new ArrayList<NameValuePair>();
			 lists.add(new NameValuePair("username","qq1121212159"));
			 webRequests.setHttpMethod(HttpMethod.POST);
			 webRequests.setRequestParameters(lists);
			 UnexpectedPage pagetxt= webClient.getPage(webRequests);

			 if(!pagetxt.getWebResponse().getContentAsString().contains("false")){
				 
		
						 WebRequest webRequest=new  WebRequest(new java.net.URL("https://login.taobao.com/member/login.jhtml"));
						 webClient.addRequestHeader("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0");
						 webClient.addRequestHeader("accept", "*/*");
						 webClient.addRequestHeader("accept-language", "zh-CN");
						 webClient.addRequestHeader("Accept-Encoding", "gzip, deflate");
						 List<NameValuePair> list=new ArrayList<NameValuePair>();
						 list.add(new NameValuePair("TPL_password","asd"));//此时这个密码 可以随意
						 list.add(new NameValuePair("TPL_username",username));//帐号
						 list.add(new NameValuePair("newlogin", "1"));
						 list.add(new NameValuePair("callback", "1"));
					
						 webRequest.setHttpMethod(HttpMethod.POST);
						 webRequest.setRequestParameters(list);
						 HtmlPage pagess=webClient.getPage(webRequest);
						 if(pagess.getTitleText().equals("页面跳转中")){
							 System.out.println("进来了");
							 System.out.println(pagess.asXml());
						 }else{

							 	if(pagess.asXml().contains("请按住滑块,拖动到最右边")){
							 		System.out.println("可以破解");
							 		//执行滑动JS
							 		HtmlPage result= (HtmlPage) pagess.executeJavaScript(" var event = document.createEvent('MouseEvents');event.initMouseEvent('mousedown', true, true, document.defaultView,0,0,0,0,0, false, false, false, false, 11 ,null); nc_1_n1z.dispatchEvent(event);var event = document.createEvent('MouseEvents');event.initMouseEvent('mousemove', true, true, document.defaultView, 0,0,0, 290,290, false, false, false, false,0,null);nc_1_n1z.dispatchEvent(event);").getNewPage();
							 			Thread.sleep(1900);
							 		System.out.println(result.asXml());
							 		System.out.println(result.getUrl());
							 
								 //获得密码狂输入
//									HtmlPasswordInput pagesy= (HtmlPasswordInput) pageyz.getElementById("TPL_password_1");
//							 		pagesy.setValueAttribute("xxx");
//							 		//获得按钮提交
//									HtmlButton  button=(HtmlButton) pageyz.getElementById("J_SubmitStatic");
//									HtmlPage pagev= button.click();
//									Thread.sleep(3000);
//									System.out.println(pagev.asXml());
//									
							 		
							 	}else{
							 		System.out.println("找不到拖动验证码");
							 	}
							
								 
							 }

							 if(count==3&&flg==true){
								 System.out.println("读取出错");
							 }
		
						 
			 }else{
				 System.out.println("需要验证码");
			 }
			
				 
		
			
			
 
}
 

}

 

<无标签>
举报
伟仔12541
发帖于1年前 0回/631阅
顶部