- 浏览: 76412 次
- 性别:
- 来自: 北京
最新评论
-
Foxswily:
huang_yong 写道XStream 对于 List< ...
XStream转换JSON要注意的地方 -
huang_yong:
XStream 对于 List<Map<Strin ...
XStream转换JSON要注意的地方 -
ricien:
我登录人人之类的都登录不上去,无法跳转请问一下是什么问题啊
HtmlUnit实现的网站登录 -
Foxswily:
jiangshuiy 写道JsonHierarchicalSt ...
XStream转换JSON要注意的地方 -
jiangshuiy:
JsonHierarchicalStreamDriver,可是 ...
XStream转换JSON要注意的地方
最近坛子里接连出现基于httpclient登录网站的帖子,也凑个热闹,分享一点基于htmlunit的登录经验
谨以此文祭奠我刚刚逝去的鼠标
----------------------------------------------分割线---------------------------------------------------
HtmlUnit 目前最新版本2.7(2010-04-15 Foxswily本人确认)
基于httpclient封装(甚至已经做好启用httpclient4的准备),模拟浏览器操作,JavaScript支持较全面,包括主流的jQuery类库,这也是它的强大之处,一般网站的JS屏蔽可以轻松突破。
举例说明
//创建浏览器,可以选择IE、FF等等 WebClient client = new WebClient(BrowserVersion.INTERNET_EXPLORER_7); //获取某网站页面 HtmlPage page = client.getPage("http://xxx.com"); //获取某页面元素,可通过id或name,(具体方式很多 --Foxswily) HtmlElement elmt = page.getElementById("someid"); //HtmlElement elmt = page.getElementByName("somename"); //此例以文本框为例,先点击,再输入,完全跟真浏览器行为一致 elmt.click(); elmt.type("somewords"); //获取按钮 HtmlButton loginBtn = (HtmlButton)page.getElementById("btnId"); //点击并获得返回结果 Page resultPage = loginBtn.click(); //结果拿到了,想干啥您随意 log.debug(resultPage.getWebResponse().getContentAsString());
沿着这个思路展开一下,模拟登录不再需要破解什么js逻辑,用户实际做什么代码就模拟什么,轻松多了
额外的友情提示,Foxswily本人曾在登录用户量众多的discuz论坛时发现个小问题(已提交bug)
造成登录后跳转失效,如有雷同参照解决吧
问题描述
HtmlPage.executeRefreshIfNeeded()
when html header has meta like "<META HTTP-EQUIV="Refresh" CONTENT="3 URL=h
ttp://www.some.org/some.html">" it throws NumberFormatException.
cause there is no ";" after "3" in the content.
some forum sites have this bad writting html page.
大意就是,自动跳转格式有问题,htmlunit解析不了,直接Exception了,改写HtmlPage的一个方法后通过。
private void executeRefreshIfNeeded() throws IOException { // If this page is not in a frame then a refresh has already happened, // most likely through the JavaScript onload handler, so we don't do a // second refresh. final WebWindow window = getEnclosingWindow(); if (window == null) { return; } final String refreshString = getRefreshStringOrNull(); if (refreshString == null || refreshString.length() == 0) { return; } final double time; final URL url; int index = refreshString.indexOf(";"); final boolean timeOnly = (index == -1); if (timeOnly && refreshString.indexOf(" ") == -1) { // Format: <meta http-equiv='refresh' content='10'> try { time = Double.parseDouble(refreshString); } catch (final NumberFormatException e) { if (LOG.isErrorEnabled()) { LOG.error("Malformed refresh string (no ';' but not a number): " + refreshString, e); } return; } url = getWebResponse().getRequestSettings().getUrl(); } else { if (refreshString.indexOf(";") == -1) { index = refreshString.indexOf(" "); } // Format: <meta http-equiv='refresh' // content='10;url=http://www.blah.com'> try { time = Double.parseDouble(refreshString.substring(0, index).trim()); } catch (final NumberFormatException e) { if (LOG.isErrorEnabled()) { LOG.error("Malformed refresh string (no valid number before ';') " + refreshString, e); } return; } index = refreshString.toLowerCase().indexOf("url=", index); if (index == -1) { if (LOG.isErrorEnabled()) { LOG.error("Malformed refresh string (found ';' but no 'url='): " + refreshString); } return; } final StringBuilder buffer = new StringBuilder(refreshString .substring(index + 4)); if (buffer.toString().trim().length() == 0) { // content='10; URL=' is treated as content='10' url = getWebResponse().getRequestSettings().getUrl(); } else { if (buffer.charAt(0) == '"' || buffer.charAt(0) == 0x27) { buffer.deleteCharAt(0); } if (buffer.charAt(buffer.length() - 1) == '"' || buffer.charAt(buffer.length() - 1) == 0x27) { buffer.deleteCharAt(buffer.length() - 1); } final String urlString = buffer.toString(); try { url = getFullyQualifiedUrl(urlString); } catch (final MalformedURLException e) { if (LOG.isErrorEnabled()) { LOG.error("Malformed URL in refresh string: " + refreshString, e); } throw e; } } } final int timeRounded = (int) time; getWebClient().getRefreshHandler().handleRefresh(this, url, timeRounded); }
评论
<frame src="blank.jsp" scrolling="no" noresize>
<frameset rows="90,*" cols="*" frameborder="NO" border="0" framespacing="0">
<frame src="header.jsp" rows="90,*" scrolling="no" noresize name="header" >
<frameset cols="200,*" frameborder="NO" border="0" framespacing="0">
<frame src="menu.faces" scrolling="auto" noresize name="menu" >
<frame src="./SysManager/bulletin.faces?bbstype=2" scrolling="auto" noresize name="body" >
</frameset>
</frameset>
我登录成功以后返回到frame框架这里,不知道如何往下写了》。。。。
那个问题用htmlpage.executeJavaScript(script code)解决了,但最近遇到一个错误(用的是HtmlUnit 2.8):
警告: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://www.ibcbet.com/commjs/ieupdate.js', but got 'application/x-javascript'.
似乎是个BUG,htmlunit作者承认的
比如:<a href="#" click="return submit()"></a>
还有没搞清楚,为什么HtmlForm没有直接submit的方法。。。
第二个问题,HttpClient和HtmlUnit不是一个层次上的,HtmlUnit底层使用了HttpClient,他方便的地方在于Html和JS的解析。
<form id="f_login" name="f_login" action="" target="_self" method="post" onsubmit="return checkInput();"> <input type="hidden" name="redirect" value="http://www.nate.com"> <input type="hidden" id="PASSWD_RSA" name="PASSWD_RSA" value=""> <fieldset> <legend>로그인</legend> <dl> <dt>로그인</dt> <dd> <input type="text" id="ID" name="ID" class="bg_id" maxlength="25" onclick="this.className='bg';" onKeyDown="this.className='bg';" tabindex="1" title="아이디 입력" onfocus="xXecure.showCKKeyProPopup();" onblur="xXecure.hideCKKeyProPopup();" /> @ <select id="domain" name="domain" tabindex="2"><!-- 2009.07 : ID 추가 --> <option selected="selected">nate.com</option> <option>empas.com</option> <option>lycos.co.kr</option> <option>netsgo.com</option> </select> </dd> <dd> <input type="password" id="PASSWD" name="PASSWD" class="bg_passwd" maxlength="20" onclick="this.className='bg';" onKeyDown="this.className='bg';" tabindex="3" title="비밀번호 입력" onkeypress="if (13 == event.keyCode) submit();" onfocus="xXecure.showCKKeyProPopup();" onblur="xXecure.hideCKKeyProPopup();" enc="on" /> </dd> <dd class="check"> <input type="checkbox" id="saveid" name="saveid" /> <label for="saveid" id="" tabindex="4">아이디 저장</label> <a class="btn_otp" onclick="xXecurePop.openWin(2);" title="OTP보안 안내">OTP보안 안내</a> </dd> <dd class="btn"> <input type="submit" tabindex="6" title="로그인버튼" value=""/> </dd> </dl> </fieldset> </form>
底下这部分是用来提交这个form的
<input type="submit" tabindex="6" title="로그인버튼" value=""/>
就是什么126邮箱批量注册之内的
说白了就是把验证码直接显示到软件界面上去,然后留个输入框让你手工输入,然后手工点提交,程序自动完成其他元素的填写。明白?
1.图像识别,这个可以单独拿来研究了,算法难度不小,外加现在的图片干扰越来越邪乎,不好实现。
2.显示图片人工解决,个人推荐这方式。毕竟登录一次可以程序保障长期在线,性价比高的方案:)
第二个解决方法,能不能帖段代码...
1,把登陆页面刷出来。
2,肉眼识别出验证码。
3,把验证码写死到程序里去。
是不是这样
1.图像识别,这个可以单独拿来研究了,算法难度不小,外加现在的图片干扰越来越邪乎,不好实现。
2.显示图片人工解决,个人推荐这方式。毕竟登录一次可以程序保障长期在线,性价比高的方案:)
第二个解决方法,能不能帖段代码...
1.图像识别,这个可以单独拿来研究了,算法难度不小,外加现在的图片干扰越来越邪乎,不好实现。
2.显示图片人工解决,个人推荐这方式。毕竟登录一次可以程序保障长期在线,性价比高的方案:)
这个貌似一个不错的测试工具.
发表评论
-
轻量slideshow(by markdown、pandoc、revealjs)
2013-12-30 16:44 4999又到写年终总结的时候了,用笨重的ppt写记录文字实在得不偿 ... -
小心JRE升级带来的麻烦
2011-04-06 15:56 7303Windows 2003的服务器部署了Tomcat(6.0.2 ... -
升级 Tomcat 6.0.24+ 享用内存溢出警告
2010-12-09 09:57 2296项目需要对Tomcat下部署的context做 ... -
maven filter转换时容易忽略的错误
2010-11-08 10:19 3178使用Maven管理,定义了Filter,配置文件中的动态值都由 ... -
想要不那么Bad smell 的 UnitTest真不容易
2010-10-27 11:19 1437自打用Maven做项目管理之后,直接和UnitTest接触的次 ... -
注释也可以很有爱——Top funny source code comments
2010-09-20 13:57 1261/* * OK; before you read the ... -
总结几点Quartz的经验
2010-06-03 14:18 7240总结一下经典的定时处理框架quartz的使用经验。基于 Ver ... -
logback的SizeBasedTriggeringPolicy
2010-06-01 15:32 1974slf4j+logback基本已经完全替代了log4j,偶然一 ... -
HtmlUnit 2.6处理页面刷新的bug
2009-10-29 14:37 3324HtmlUnit 2.6做了大量针对Javascript的工作 ... -
开心网外挂编写总结
2009-09-29 11:12 1423总结写开心外挂的心得 ...
相关推荐
htmlunit 模拟cookie 存取 cookie值登录 如: wsyyuser.xywy.com Cookie c = new Cookie("wsyyuser.xywy.com", "PHPSESSID", Common.getValue("session_id") );
htmlunit java版无界面浏览器 实现网页自动登录利器 官方最新下载 htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。项目可以模拟浏览器运行,被誉为java浏览器的开源...
protocol-htmlunit: 基于Htmlunit实现的AJAX页面Fetcher插件 parse-s2jh: 基于XPath解析页面元素内容; 基于数据库模式输出解析到结构化数据; 对于个别复杂类型AJAX页面定制判断页面加载完成的回调...
htmlUnit所需工具包,htmlUnit,htmlUnit,htmlUnit,htmlUnit,htmlUnit
通过htmlunit 下载网站资源的简单示例 htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。 项目可以模拟浏览器运行,被誉为java浏览器的开源实现。是一个没有界面的...
htmlunit jar zip 2.23 用于html爬虫
htmlunit2.8.jar
htmlunit-2.1.5的源码,可以提高我们对htmlunit的理解
htmlunit爬取网站的数据并保存成Excel表格
这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本...
java 开发 htmlunit官方jar包和api文档
htmlunit-2.19-bin
htmlunit用到的jar包htmlunit用到的jar包htmlunit用到的jar包
htmlunit2.8 + jsoup1.7各种网站上的数据抓取。
htmlunit包
赠送jar包:htmlunit-2.33.jar; 赠送原API文档:htmlunit-2.33-javadoc.jar; 赠送源代码:htmlunit-2.33-sources.jar; 赠送Maven依赖信息文件:htmlunit-2.33.pom; 包含翻译后的API文档:htmlunit-2.33-javadoc-...
htmlunit爬虫工具,Java所需的依赖jar包,凑够50个字符
htmlunit2.9all full jar
赠送jar包:htmlunit-2.49.1.jar; 赠送原API文档:htmlunit-2.49.1-javadoc.jar; 赠送源代码:htmlunit-2.49.1-sources.jar; 赠送Maven依赖信息文件:htmlunit-2.49.1.pom; 包含翻译后的API文档:htmlunit-2.49....
htmlunit所需要的所有的jar,亲测可用,包含htmlunit,htmlunit-core-js等等