在用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)

本文介绍了初学者在使用jsoup爬取晋江网站时遇到的问题,即在模拟登录后无法获取到JavaScript动态加载的v章内容。为解决这个问题,作者通过结合htmlunit和jsoup,成功实现了对JavaScript内容的解析和抓取。
最低0.47元/天 解锁文章
1048

被折叠的 条评论
为什么被折叠?



