如何在Java中使用JSOUP得到任何网页的DOM树动态内容 [英] How to get dynamic contents of any web page in DOM tree using JSOUP in Java
问题描述
在我的项目,该项目解析HTML页面,然后使用DOM树进行不同的操作,就像,比较两个URL的模板。
对于这一点,我使用的 JSOUP
但它不能够加载动态内容的DOM树。
您可以告诉我怎么可以加载在Java中使用JSOUP动态内容,或者你可以告诉我任何其他方法做同样的?
修改NO。 1
作为<一个href="http://stackoverflow.com/questions/12625043/getting-jsoup-to-support-dynamically-generated-html-by-javascript">given链接显示,它的工作原理使用的 PhantomJS 和 Zombie.js 在Java中。你能告诉我怎样才能做到这一点?
修改第2号
我第一次尝试使用硒来获得动态网页,而code如下,
公共静态无效的主要(字串[] args)抛出IOException异常{
// 硒
webdriver的驱动程序=新FirefoxDriver();
driver.get(任何URL HERE);
串html_content = driver.getPageSource();
driver.get(另一个URL HERE);
串html_content1 = driver.getPageSource();
driver.close();
// Jsoup使得DOM这里通过解析HTML内容
文档DOC1 = Jsoup.parse(html_content);
文档DOC2 = Jsoup.parse(html_content1);
使用DOM树//营运
}
但是,这也优化后,需要大量的时间。现在按照您的指示,我搬到的HtmlUnit。 但我不能够使code,是可以获得动态页面源$ C $ C成字符串,然后我用这个字符串作进一步配对使用Jsoup,帮我用的HtmlUnit编写code。
使用的HtmlUnit code 的: -
包XXX.YYY.ZZZ.Template_Matching;
进口com.gargoylesoftware.htmlunit.WebClient;
进口com.gargoylesoftware.htmlunit.html.HtmlPage;
进口org.junit.Assert;
进口org.junit.Test;
/ **
*
* @author jhamb
* /
公共类{的HtmlUnit
@测试
公共无效网页()抛出异常{
最终的Web客户端Web客户端=新的Web客户端();
最终HtmlPage页= webClient.getPage(http://www.jabong.com/Yepme-3-4Th-Sleeve-Printed-Blue-Top-Mksp-191481.html);
文件HT = page.getOwnerDocument();
的System.out.println(HT);
webClient.closeAllWindows();
}
公共静态无效的主要(字串[] args)抛出异常{
的HtmlUnit的HtmlUnit =新的HtmlUnit();
htmlUnit.homePage();
}
}
我很害怕,JSoup不会在这种情况下工作。
尝试使用的HtmlUnit。
In my project, which parses the HTML page, then uses the DOM tree for different operations, just like, comparing templates of two URLS.
For that, I am using JSOUP.
But it does not able to load Dynamic contents in DOM tree.
Can you tell me how can I load dynamic content using JSOUP in Java, or can you tell me any other method for doing the same?
EDIT NO. 1
As given link shows, it works using PhantomJS and Zombie.js in Java. Can you tell me how can I do this ?
Edit No. 2
I first try to get dynamic page by using Selenium, and the code is as follows,
public static void main(String[] args) throws IOException {
// Selenium
WebDriver driver = new FirefoxDriver();
driver.get("ANY URL HERE");
String html_content = driver.getPageSource();
driver.get("ANOTHER URL HERE");
String html_content1 = driver.getPageSource();
driver.close();
// Jsoup makes DOM here by parsing HTML content
Document doc1 = Jsoup.parse(html_content);
Document doc2 = Jsoup.parse(html_content1);
// OPERATIONS USING DOM TREE
}
But this takes lots of time after optimizing also. Now as per your instructions, I moved to HtmlUnit. But I am not able to make code, that gets Dynamic Page source code into String , and then I use this String for further paring using Jsoup, help me to write that code using HtmlUnit.
Code using 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;
/**
*
* @author jhamb
*/
public class HtmlUnit {
@Test
public void homePage() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.jabong.com/Yepme-3-4Th-Sleeve-Printed-Blue-Top-Mksp-191481.html");
Document ht = page.getOwnerDocument();
System.out.println(ht);
webClient.closeAllWindows();
}
public static void main(String[] args) throws Exception {
HtmlUnit htmlUnit = new HtmlUnit();
htmlUnit.homePage();
}
}
I'm afraid, JSoup won't work in this case.
Try using HtmlUnit.
这篇关于如何在Java中使用JSOUP得到任何网页的DOM树动态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!