感谢提问!欢迎关注本人头条号!
1、简单的jsoup爬取
String url=”a.atimo.cn”;//静态页面链接地址
Document doc = Jsoup.connect(url).userAgent(“Mozilla”).timeout(4000).get();
if(doc!=null){
Elements es = doc.select(“div.comments>ul>li”);//
System.out.println(es);
if(es!=null && es.size()>0){
for (Element element : es) {
String link = element.select(“div>h3”).attr(“href”);
String title = element.select(“div>h3”).text();
String author = element.select(“div.c-abstract>em”).text();
String content = element.select(“dd>a>div.icos>i:eq(1)”).text();
}
}
}
通过jsop解析返回Document 使用标签选择器,选择页面标签中的值,即可获取页面内容。
2.延时加载,有些网站存在延时加载,表格内容,或者嵌入页面形式的加载的页面
//构造一个webClient 模拟Chrome 浏览器
String url = “https://www.cnblogs.com/atimo/”;
WebClient webClient = new WebClient(BrowserVersion.CHROME);
//支持JavaScript
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setActiveXNative(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setTimeout(3000000);
HtmlPage rootPage = webClient.getPage(url);
String html = rootPage.asXml();
Document document = Jsoup.parse(html);
Elements es = document.select(“div.comments”);//.select(“#content_left”);
System.out.println(es);
if(es!=null && es.size()>0){
for (Element element : es) {
String link = element.select(“div.f13>a”).attr(“href”);
String title = element.select(“div>h3>a”).text();
String text = element.select(“div.c-abstract>em”).text();
}
}
获取到的是Document 使用标签选择器,选择页面标签中的值,即可获取页面内容。
3.获取评论或其他内容,返回json数据;js请求
普通请求,只需要使用
HttpURLConnection connection = createRequest(url, “GET”);
// 建立实际的连接 connection.connect();
发送GET请求过去json数据后解析即可;
4.js请求带请求头参数(部分为移动端请求)
CloseableHttpClient https = HttpClients.createDefault();
String url = “https://action=hene=124&devicetype=androidlag=zh_CN&nettyene=3&pass_ticwx_header=1”;
HttpGet httpPost = new HttpGet(url);
httpPost.addHeader(“Host”, “mp.weixin.qq.com”);
httpPost.addHeader(“x-wechat-uin”, wechartCookie.getUin());
httpPost.addHeader(“x-“, “参数”);
HttpResponse response = https.execute(httpPost);
HttpEntity entitySort = response.getEntity();
String html = EntityUtils.toString(entitySort, “utf-8”);
请求头参数根据抓包工具拦截的请求时需要的参数变更;
response css,css单位教学,CSS模板素材稀有,360极速模式css宽度,css注释代码的写法,css 安卓手机无法滑动,css经过二维码