我尝试从 URL 获取动态页面。我在 Java 中工作。我已经使用 Selenium 完成了这项工作,但这需要很多时间。因为调用 Selenium 的驱动程序需要时间。这就是我转向 HtmlUnit 的原因,因为它是无 GUI 浏览器。但是我的 HtmlUnit 实现显示了一些异常。
问题:-
- 如何更正我的 HtmlUnit 实现。
- Selenium 生成的页面是否与 HtmlUnit 生成的页面相似?[ 两者都是动态的吗?]
我的硒代码是:-
public static void main(String[] args) throws IOException {
// Selenium
WebDriver driver = new FirefoxDriver();
driver.get("ANY URL HERE");
String html_content = driver.getPageSource();
driver.close();
// Jsoup makes DOM here by parsing HTML content
Document doc = Jsoup.parse(html_content);
// OPERATIONS USING DOM TREE
}
HtmlUnit 代码:-
package XXX.YYY.ZZZ.Template_Matching;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.junit.Assert;
import org.junit.Test;
public class HtmlUnit {
public static void main(String[] args) throws Exception {
//HtmlUnit htmlUnit = new HtmlUnit();
//htmlUnit.homePage();
WebClient webClient = new WebClient();
HtmlPage currentPage = webClient.getPage("http://www.jabong.com/women/clothing/womens-tops/?source=women-leftnav");
String textSource = currentPage.asText();
System.out.println(textSource);
}
}
它显示异常:-