jsoup分页

来源:互联网 发布:sql语句统计各个数量 编辑:程序博客网 时间:2024/06/07 15:46
package collect;


import java.io.IOException;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class JsoupDemo {


public static void main(String[] args) throws IOException {
//要采集的网址
String url="http://hbqj.gov.cn:8888/child_site/jwjcj/lzyw/";
Document document = Jsoup.connect(url) 
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36") 
.get();
//获取审查页面的页数信息
String page=document.select(".pages.mt30.mb10 em").text();
//截取数据
//获取总的条数
int start=page.indexOf("共");
int end=page.indexOf("条");
Integer total_count=Integer.parseInt(page.substring(start+1, end));

//获取总页数
int start1=page.indexOf("/");
int end1=page.indexOf("页");
Integer total_page=Integer.parseInt(page.substring(start1+1, end1));

for(int current_page = 1; current_page <= total_page; current_page++){
   System.out.println("-------------------第" + current_page + "页开始-------------------------");
  if(current_page==1){
  getData(url);
  }else{
  getData(url+"index_"+current_page+".html");
  }   
   System.out.println("-------------------第" + current_page + "页结束-------------------------");
       }

}
public static void getData(String url) throws IOException{
Document document = Jsoup.connect(url) 
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36") 
.get();
    Elements items=document.select(".container__wrap>div");
    Elements elements=document.select(".news__list>li");
for (Element element : elements) {
Elements titleElements=element.select("h4 a");
String title=titleElements.text();
String link=titleElements.attr("href").trim();

Elements dateElements=element.select("div:eq(1)");
String time=dateElements.text();
System.out.println(title);
System.out.println(link);
System.out.println(time);

}
}
}
原创粉丝点击