import java.io.IOException; import java.net.MalformedURLException; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlInput; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class SinaLoginTest { public static void main(String[] args) throws Exception { WebClient client = new WebClient(BrowserVersion.FIREFOX_24); client.getOptions().setJavaScriptEnabled(true); // 默认执行js,如果不执行js,则可能会登录失败,因为用户名密码框需要js来绘制。 client.getOptions().setCssEnabled(false); client.setAjaxController(new NicelyResynchronizingAjaxController()); client.getOptions().setThrowExceptionOnScriptError(false); HtmlPage page = client .getPage("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.16)"); // System.out.println(page.asText()); // 登录 HtmlInput ln = page.getHtmlElementById("username"); HtmlInput pwd = page.getHtmlElementById("password"); HtmlInput btn = page .getFirstByXPath(".//*[@id='vForm']/div[3]/ul/li[6]/div[2]/input"); ln.setAttribute("value", "159890555555"); pwd.setAttribute("value", "nmj121"); HtmlPage page2 = btn.click(); // 登录完成,现在可以爬取任意你想要的页面了。 System.out.println("\n\n\n"); // System.out.println(page2.asText()); HtmlPage page3 = client.getPage("http://weibo.com/friends?leftnav=1&wvr=5&isfriends=1&step=2"); System.out.println(" : " + page3.asXml()); client.closeAllWindows(); } }
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
FIREFOX_24 cannot be resolved or is not a field
The method getOptions() is undefined for the type WebClient
The method getOptions() is undefined for the type WebClient
NicelyResynchronizingAjaxController cannot be resolved to a type
The method getOptions() is undefined for the type WebClient
The method getPage(URL) in the type WebClient is not applicable for the arguments (String)
Type mismatch: cannot convert from HtmlElement to HtmlInput
Type mismatch: cannot convert from HtmlElement to HtmlInput
The method getFirstByXPath(String) is undefined for the type HtmlPage
The method setAttribute(String, String) is undefined for the type HtmlInput
The method setAttribute(String, String) is undefined for the type HtmlInput
Type mismatch: cannot convert from Page to HtmlPage
The method getPage(URL) in the type WebClient is not applicable for the arguments (String)
The method closeAllWindows() is undefined for the type WebClient
at com.qjj.test.SinaLoginTest.main(SinaLoginTest.java:14)
感觉是运行期你使用的第三方库没有进入classpath
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。