我想抓取一个网站,但是当我使用 Jsoup.connect(url)
连接到它时仅加载页面的一部分。
当我将页面下载为 html 时,我看到页面的一部分只有一个加载器图标,因此我得出结论,页面的该部分是后来从其他来源加载的。
有趣的是,检查元素包含缺少的 html,而查看页面源代码则不包含。从 jSoup 加载的 HTML 与从“查看页面源代码”打开时基本相同。
有没有办法绕过这个并加载浏览器中显示的整个页面?
有问题的页面是这样的:https://www.oddsportal.com/tennis/australia/atp-australian-open-2017/results/page/1/
询问我可以提供的任何其他信息。
==============
编辑: 我正在连接到这样的网址:
Document doc = null;
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
我使用 css 选择器获取这个 div:
Elements tournamentTable = doc.select("div[id=tournamentTable]");
tournamentTable
的内容是 <div id="tournamentTable"></div>
最佳答案
看来id=tournamentTable
是使用javascript动态生成的。 JSoup 不评估 javascript,因此您必须使用像 HtmlUnit 这样的库。例如:
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true); // enable javascript
webClient.getOptions().setThrowExceptionOnScriptError(false); //even if there is error in js continue
webClient.waitForBackgroundJavaScript(5000); // important! wait until javascript finishes rendering
HtmlPage page = webClient.getPage(url);
page.getElementById("tournamentTable");
关于java - JSoup 不加载整个 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101049/