当运行到page = (HtmlPage) webClient.getPage(url);的时候不继续运行下一句代码而且也不停止; 我也设置了 webClient.getOptions().setCssEnabled(false); webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.setAjaxController(new NicelyResynchronizingAjaxController());
贴代码 7 public class TEST { public static void main(String[] args) { WebClient webClient=new WebClient(BrowserVersion.FIREFOX_17); webClient.getOptions().setCssEnabled(false); webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.setAjaxController(new NicelyResynchronizingAjaxController()); HtmlPage page; try { // 构造一个URL String url = new String("http://www.facebookcover4u.com/"); // 通过getPage()方法,返回相应的页面 page = (HtmlPage) webClient.getPage(url); //写入文件// List<HtmlAnchor> urlList = new ArrayList<HtmlAnchor>(); urlList.addAll(page.getAnchors()); Iterator<HtmlAnchor> it= urlList.iterator(); //test String fileName = "D:\\Result2.txt"; FileWriter fw = new FileWriter(new File(fileName),true); BufferedWriter bfw = new BufferedWriter(fw); while(it.hasNext()){ HtmlAnchor ha = it.next(); bfw.write(ha+"\n"); } //test bfw.flush(); fw.flush(); bfw.close(); fw.close(); webClient.closeAllWindows(); } catch (FailingHttpStatusCodeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 可以通过webClient.setJavaScriptTimeout(5000);这句话可以讲javascript的处理时间设为5秒,程序就不会死在这儿了
|