JAVA使用HtmlUnit爬虫工具模拟登陆CSDN案例
程序员文章站
2024-03-04 18:16:00
最近要弄一个爬虫程序,想着先来个简单的模拟登陆, 在权衡jxbrowser和htmlunit 两种技术, jxbowser有界面呈现效果,但是对于某些js跳转之...
最近要弄一个爬虫程序,想着先来个简单的模拟登陆, 在权衡jxbrowser
和htmlunit
两种技术, jxbowser
有界面呈现效果,但是对于某些js跳转之后的效果获取比较繁琐。
随后考虑用htmlunit
, 想着借用咱们csnd的登陆练练手。谁知道csdn的登陆,js加载时间超长,不设置长一点的加载时间,按钮提交根本没效果,js没生效。 具体看代码注释吧。 奉劝做爬虫的同志们,千万别用csdn登陆练手,坑死我了。
maven
配置如下:
<dependencies> <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit --> <dependency> <groupid>net.sourceforge.htmlunit</groupid> <artifactid>htmlunit</artifactid> <version>2.18</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency> <groupid>org.jsoup</groupid> <artifactid>jsoup</artifactid> <version>1.9.2</version> </dependency> </dependencies>
代码如下:
package com.test; import java.io.ioexception; import java.net.malformedurlexception; import java.util.hashmap; import java.util.map; import java.util.set; import com.gargoylesoftware.htmlunit.browserversion; import com.gargoylesoftware.htmlunit.failinghttpstatuscodeexception; import com.gargoylesoftware.htmlunit.nicelyresynchronizingajaxcontroller; import com.gargoylesoftware.htmlunit.silentcsserrorhandler; import com.gargoylesoftware.htmlunit.webclient; import com.gargoylesoftware.htmlunit.html.htmlbuttoninput; import com.gargoylesoftware.htmlunit.html.htmlform; import com.gargoylesoftware.htmlunit.html.htmlpage; import com.gargoylesoftware.htmlunit.html.htmlpasswordinput; import com.gargoylesoftware.htmlunit.html.htmltextinput; import com.gargoylesoftware.htmlunit.util.cookie; public class simulatelogin { //访问的目标网址(csdn) private static string target_url = "https://passport.csdn.net/account/login?from=http://www.csdn.net"; public static void main(string[] args) throws failinghttpstatuscodeexception, malformedurlexception, ioexception { // 模拟一个浏览器 webclient webclient = new webclient(browserversion.chrome); // 设置webclient的相关参数 webclient.setcsserrorhandler(new silentcsserrorhandler()); //设置ajax webclient.setajaxcontroller(new nicelyresynchronizingajaxcontroller()); //设置支持js webclient.getoptions().setjavascriptenabled(true); //css渲染禁止 webclient.getoptions().setcssenabled(false); //超时时间 webclient.getoptions().settimeout(50000); //设置js抛出异常:false webclient.getoptions().setthrowexceptiononscripterror(false); //允许重定向 webclient.getoptions().setredirectenabled(true); //允许cookie webclient.getcookiemanager().setcookiesenabled(true); // 模拟浏览器打开一个目标网址 htmlpage page = webclient.getpage(target_url); /**等待js加载完全,csdn这点 特别坑,js加载时间超长!!!!!!! 后人切记不要用csdn模拟登陆!!!!!!!**/ webclient.waitforbackgroundjavascript(10000*3); // 根据form的名字获取页面表单,也可以通过索引来获取:page.getforms().get(0) htmlform form = (htmlform) page.getelementbyid("fm1"); htmltextinput username = (htmltextinput) form.getinputbyname("username"); htmlpasswordinput password = (htmlpasswordinput) form.getinputbyname("password"); username.setvalueattribute("********"); //用户名 password.setvalueattribute("********"); //密码 htmlbuttoninput button = (htmlbuttoninput) page.getbyxpath("//input[contains(@class, 'logging')]").get(0); // scriptresult result = page.executejavascript("javascript:document.getelementsbyclassname('logging')[0].click()"); // htmlpage retpage = (htmlpage) result.getnewpage(); htmlpage retpage = button.click(); // 等待js驱动dom完成获得还原后的网页 webclient.waitforbackgroundjavascript(1000); //输出跳转网页的地址 system.out.println(retpage.geturl().tostring()); //输出跳转网页的内容 system.out.println(retpage.asxml()); //获取cookie set<cookie> cookies = webclient.getcookiemanager().getcookies(); map<string, string> responsecookies = new hashmap<string, string>(); for (cookie c : cookies) { responsecookies.put(c.getname(), c.getvalue()); system.out.print(c.getname()+":"+c.getvalue()); } webclient.close(); system.out.println("success!"); } }
另外,csdn的js总是莫名其妙的报一堆错,如果不想看,想忽略的话,在创建webclient
前加上如下代码:
//设置日志级别,原页面js异常不打印 logfactory.getfactory().setattribute("org.apache.commons.logging.log", "org.apache.commons.logging.impl.nooplog"); java.util.logging.logger.getlogger("com.gargoylesoftware.htmlunit") .setlevel(level.off); java.util.logging.logger.getlogger("org.apache.commons.httpclient") .setlevel(level.off);
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接