htmlunit执行速度慢怎么办?

牧码Yin 发布于 2018/06/13 16:47
阅读 1K+
收藏 0

NGINX Sprint China 2022--NGINX年度旗舰盛会,已开启免费预约!>>>

 这是我的代码,这句代码:HtmlPage htmlpage = webclient.getPage("https://baike.baidu.com/");执行速度特别慢,谁知道是什么情况啊?

public static String Baidu(String keyword)throws Exception{
		WebClient webclient = new WebClient();
		webclient.getOptions().setThrowExceptionOnScriptError(false);
		webclient.getOptions().setThrowExceptionOnFailingStatusCode(false);
		webclient.getOptions().setCssEnabled(false);
		webclient.getOptions().setJavaScriptEnabled(true);   
//		//ssl认证
		webclient.getOptions().setUseInsecureSSL(true);
		HtmlPage htmlpage = webclient.getPage("https://baike.baidu.com/");
		HtmlForm form = htmlpage.getForms().get(0);
		HtmlButton button = htmlpage.getHtmlElementById("search");
		HtmlTextInput textField = form.getInputByName("word");
		textField.setValueAttribute(keyword);
//		final HtmlSelect htmlSelet=form.getSelectByName("rn");
//		htmlSelet.setDefaultValue("10");
//		final HtmlHiddenInput hiddenInputtn = form.getInputByName("tn");
//		hiddenInputtn.setDefaultValue("baiduadv");

		final HtmlPage page = button.click();
		String url = page.getUrl().toString();
		System.out.println(page.asText());
//		Document doc = Jsoup.parse(page.getUrl(), 60000); 
//		Elements element = doc.getElementsByClass("t c-gap-bottom-small");
//		page.get
		DomNodeList<DomElement> list = page.getElementsByTagName("div");
		String result ="";
		for (DomElement domElement : list) {
		    //根据目标元素class属性循环匹配
		    if (domElement.getAttribute("class").equals("lemma-summary")) {
		    	result = domElement.asText();
		    	break;
		    }
		}
		
		
		
		//HtmlPage page2 = page.getAnchorByName().click();
		// System.out.println("----"+page2.asText());
		//String result = page.asXml();
		//result = result.split("百度百科")[1].split("。")[0];
		System.out.println(result);
		webclient.closeAllWindows();
		return result;
	}

 

加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部