扒图

2016-12-23  本文已影响0人  liuzhongwei
public static void main(String[] args){
    
  for(int j = 1;j<10;j++){//循环页数
            //解析document
            Document document = Jsoup.connect("http://www.lanrentuku.com/s.php?keyword=%C3%C0%C5%AE&searchtype=titlekeyword&channeltype=0&orderby=&kwtype=0&pagesize=21&typeid=1&TotalResult=134&PageNo="+j)
                    .cookie("is_click","1")
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36")
                    .get();
            Elements elements = document.select(".list-qq dl dd a");//解析document确定位置标签唯一
            for(int i= 0;i<elements.size();i++){
                String url = elements.get(i).attr("href");
                System.out.println(url);
                //再解析document
                Document documents = Jsoup.connect("http://www.lanrentuku.com/"+url)
                        .cookie("is_click","1")
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36")
                        .get();
                Element elementss = documents.select(".content-a p img").first();//再解析确定需要标签
                String src = elementss.attr("src");
                System.out.println(src);
                CloseableHttpClient httpClient = HttpClients.createDefault();
                HttpGet httpGet = new HttpGet(src);
                HttpResponse response = httpClient.execute(httpGet);
                if(response.getStatusLine().getStatusCode() == 200){
                    //响应输入流,拿到此响应的东西
                    InputStream inputStream = response.getEntity().getContent();
                    //文件名
                    String imgname = src.substring(src.lastIndexOf("/"));
                    FileOutputStream outputStream = newFileOutputStream("D:/lanren/"+imgname);
                    IOUtils.copy(inputStream,outputStream);
                    inputStream.close();
                    outputStream.flush();
                    outputStream.close();
                } else {
                    System.out.print("ERROR:"+ response.getStatusLine().getStatusCode());
                }
            }
        }
        
    }
}
上一篇 下一篇

猜你喜欢

热点阅读