本人使用Jsoup框架爬取了一个Vue网页,输出发现body里没有内容,上网上查阅发现的内容需要js发送ajax请求获取,Jsoup框架不支持js渲染网页,我便使用了HtmlUnit框架模拟一个浏览器,结果报错Default values in destructuring declarations are not supported (https://jctz.12309.gov.cn/assets-22-12/js/chunk-vendors-legacy.0dd94fd1.js#14)
弄了好几天始终没有得到解决求资深人士帮忙看看!:
这个是爬取过程的代码
public class collection3 {
public static void main(String[] args) throws IOException {
String nowHtml = "https://jctz.12309.gov.cn/main/live-details/991686541979";
//实例化web客户端
WebClient webclient = new WebClient(BrowserVersion.CHROME);
webclient.getOptions().setUseInsecureSSL(true);
// 禁用css ,一般来说 css没啥用
webclient.getOptions().setCssEnabled(true);
webclient.getOptions().setThrowExceptionOnFailingStatusCode(false);
// 设置支持 js
webclient.getOptions().setJavaScriptEnabled(true);
// 不抛出异常
webclient.getOptions().setThrowExceptionOnScriptError(false);
webclient.getOptions().setDoNotTrackEnabled(true);
// 最好设置一下超时
webclient.getOptions().setTimeout(5*1000);
// 支持ajax
webclient.setAjaxController(new NicelyResynchronizingAjaxController());
//解析获取页面
HtmlPage page = webclient.getPage("https://jctz.12309.gov.cn/main/live-details/991686541979");
// TODO: 2019/1/4 10秒钟是为了让js能够充分执行(特别是ajax)
webclient.waitForBackgroundJavaScript(10*1000);
webclient.setJavaScriptTimeout(5*1000);
System.out.println(page.asXml());
}
}
用Selenium不香吗?
```python
from selenium import webdriver
def test_grap_page(self):
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://jctz.12309.gov.cn/main/live-details/991686541979')
page = driver.page_source
with open('test.html','wb+') as file:
file.write(page.encode())
print(page)
```
解决了记得采纳一下~