在用jsoup爬晋江的时候,模拟登陆后仍获取不到v章内容。经验证,是因为jsoup无法解析JavaScript,而v章内容是js动态获取的。经过查阅资料,最后使用htmlunit+jsoup来实现
登陆和获取cookies在上一篇已经写了,不再赘述。
public static String getChapterContent(Chapter chapter, String novelUrl) {
System.out.println("正在获取第"+chapter.getChapterNum()+"章 "+chapter.getChapterTitle());
//如果是锁章,返回
if (chapter.getUrl() == null) {
return chapter.getContent();
}
//模拟浏览器
WebClient webClient = new WebClient(BrowserVersion.CHROME);
//设置请求头、cookies、代理
WebRequest request = null;
try {
request = new WebRequest(new URL(chapter.getUrl()));
} catch (MalformedURLException e2) {
e2.printStackTrace();
}
request.setProxyHost(ip);//设置代理
request.setProxyPort(port)