java-爬取手机高清壁纸(你懂得)

话不多说 上代码

public class DownWallpaper extends JFrame implements ActionListener{

    private JButton down = null;
    
    public DownWallpaper(){
        down =  new JButton("下载手机壁纸");
        down.setFont(new Font("微软雅黑",Font.ITALIC,20));
        down.addActionListener(this);
        this.setResizable(false);
        this.add(down);
        this.setTitle("高清壁纸下载");
        this.setSize(250, 150);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
  @Override
    public void actionPerformed(ActionEvent e) {
       if (e.getSource()==down) {
           try {
               JOptionPane.showMessageDialog(this, "正在下载请勿关闭主窗体!");
               JOptionPane.showMessageDialog(this, "详细信息请查看:https://sj.enterdesk.com/");
               JOptionPane.showMessageDialog(this, "下载完成后请到:d:/img下查看");
            load("https://sj.enterdesk.com/");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
           
       } 
    }
    public static void main(String[] args) {
         new DownWallpaper();
    
    }
    
    
    /**
     * 加载链接
     * @param urls
     * @throws Exception
     */
    public static void load(String urls) throws Exception{
        Connection connect = Jsoup.connect(urls);
        Document document = connect.get();
        Elements links = document.getElementsByTag("img");
        //循环爬取图片
        for(Element link : links){
            String url = link.attr("src");//下载的url
            String endWith = url.substring(url.lastIndexOf("."));//文件后缀名
            String fileName = link.absUrl("alt").substring(link.absUrl("alt").lastIndexOf("/"));//文件名
            download(url,endWith,fileName);
        }
        //获取所有的<li>
        Elements select = document.select("a");
        Elements addClass = select.addClass("next_p");
        for (Element element : addClass) {
            if (element.text().equals("下一页")) {
                //获取超链接
                String attr = element.attr("href");
                //递归循环下载
                load(attr);
            }else{
                continue;
            }
              
        }
       
    }
    
    /**
     * 下载图片
     * @param url
     */
    public static void download(String url,String endWith,String fileName) throws Exception{
            File file = new File("d:/img/");
            if (!file.exists()){
                file.mkdir();
            }else{
                file.delete();
                file.mkdir();
            }
            URL url2 = new URL(url);
            InputStream is = url2.openConnection().getInputStream();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getPath()+fileName+endWith));
            byte[] bs = new byte[2048*2048];
            while((is.read(bs))!=-1){
                bos.write(bs);
            }
            bos.flush();
           if(is != null) is.close();
           if(bos != null) bos.close();
    }
}

小生初来乍到 有什么写的不好的地方 请大佬们多多指教 感谢感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值