Java简单爬虫 jsoup工具包

首先导入一个爬虫的工具包: jsoup-1.13.1.jar

	//测试爬虫的网址(爬取王者荣耀英雄的网址)
    static String url="https://pvp.qq.com/web201605/herolist.shtml";
    //文件存放的地址
    static String path="D://爬虫测试/";
public static void getImgs(String url){
        //加载对应网址上的Html代码  Jsoup.connect()获取连接
        try {
            Document document= Jsoup.connect(url)
                    .userAgent("Mozilla")
                    .maxBodySize(0)
                    .timeout(500000)
                    .get();
       //   System.out.println(document);
//            获取显示图片的ul标签
       Elements selectUL=document.select("[class=herolist clearfix]");
        Elements selectLi=selectUL.select("li");
            for (Element element : selectLi) {
               //详情页地址
                String heroURL = element.select("a").attr("href");
              // System.out.println(heroURL);
                //获取英雄名称(标签中的文本内容)
                String heroName = element.select("a").text();
             //   System.out.println(heroName);
               String detailUrl="https://pvp.qq.com/web201605/"+heroURL;//详情页
//                获取详情页的html
                Document document2= Jsoup.connect(detailUrl).get();
                //找到显示背景的div标签
//                
Elements div = document2.select("[class=zk-con1 zk-con]"); //获取显示的背景 String bg=div.attr("style"); // System.out.println(bg); //拆分bg字符串:background:url('//game.gtimg.cn/images/yxzj/img201606/skin/hero-info/110/110-bigskin-1.jpg') center 0 String heroImgUrl=bg.substring(16,bg.length()-11);//正数第16个开始,倒数第11个结束来截取字符串 String urlname=path+heroName+".jpg";//存放的文件的名字地址 //开始下载 System.out.println("开始下载:"+heroName); //http:// download("http:"+heroImgUrl,urlname); } } catch (IOException e) { e.printStackTrace(); } }

download方法

 /**
     * 下载指定路径下的图片
     * @param
     */
    public static void download(String imgurl,String urlname){
        //构建URL链接
        try {
            URL url1 = new URL(imgurl);
            //IO流:将网页上的图片流化,输送到程序中
            //输入流
            DataInputStream dataInputStream = new DataInputStream(url1.openStream());
            //输出流:输出到本地
            FileOutputStream fileOutputStream = new FileOutputStream(urlname);
            ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
            //缓冲区
            byte[] buffer =new byte[1024];
            int length=0;
            while ((length=dataInputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,length);
            }
            fileOutputStream.write(outputStream.toByteArray());
            dataInputStream.close();
            outputStream.close();
            fileOutputStream.close();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

测试:

 public static void main(String[] args) {
        long start=System.currentTimeMillis();//系统开始时间
        getImgs(url);
        long end=System.currentTimeMillis();//系统结束时间
        System.out.println("图片下载完毕,耗时"+(end-start)/1000.0+"秒");
}

大概效果
Java简单爬虫 jsoup工具包_第1张图片

你可能感兴趣的:(java,爬虫,开发语言,前端)