前景提要
HDC调试需求开发(15万预算),能者速来!>>> 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)