java程序爬取网页上的图片

最近需要在网上找一些图片,所以写了一个爬取图片的程序,新手有写的不足之处还请各位大佬指点一二。

源代码如下

package com.sysh.ssm.service;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author:
 * @Date:2018/10/31
 */
public class SpiderPicturesFromBaiduByWord {


    public static void main(String[] args) throws Exception{
        String downloadPath = "C:\\sunhoo\\image";

        System.out.println("输入爬取关键字(可用空格,、号分隔多个想爬的关键字):");
        Scanner KeyWord = new Scanner(System.in);
        String Word =KeyWord.nextLine();
        System.out.println("输入要下载的页数(1表示一页,一页有30张图片)");
        Integer pageSize=KeyWord.nextInt();
        List<String> list = nameList(Word);
        getPictures(list,pageSize,downloadPath); //1代表下载一页,一页一般有30张图片
    }


    public static void getPictures(List<String> keywordList, int max,String downloadPath) throws Exception{ // key为关键词,max作为爬取的页数
        String gsm=Integer.toHexString(max)+"";
        String finalURL = "";
        String tempPath = "";
        //每页的数量
        Integer pagenumber=10;
        for(String keyword : keywordList){
            tempPath = downloadPath;
            if(!tempPath.endsWith("\\")){
                tempPath = downloadPath+"\\";
            }
            tempPath = tempPath+keyword+"\\";
            File f = new File(tempPath);
            if(!f.exists()){
                f.mkdirs();
            }
            int picCount = 1;
            for(int page=0;page<=max;page++) {
                sop("正在下载第"+page+"页面");
                Document document = null;
                try {
                    String url ="http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word="+keyword+"&cg=star&pn="+page*pagenumber+"&rn=30&itg=0&z=0&fr=&width=&height=&lm=-1&ic=0&s=0&st=-1&gsm="+Integer.toHexString(page*pagenumber);
                    //String url ="https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1540974009530_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%8D%8E%E5%B1%B1"+Integer.toHexString(page*30);
                    sop(url);
                    document = Jsoup.connect(url).data("query", "Java")//请求参数
                            .userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")//设置urer-agent  get();
                            .timeout(5000)
                            .get();
                    String xmlSource = document.toString();
                    xmlSource = StringEscapeUtils.unescapeHtml3(xmlSource);
                    sop("页面"+xmlSource.length());
                    String reg = "objURL\":\"http://.+?\\.jpg";
                    Pattern pattern = Pattern.compile(reg);
                    Matcher m = pattern.matcher(xmlSource);
                    sop("mmm"+m);
                    while (m.find()) {
                        finalURL = m.group().substring(9);
                        sop(keyword+picCount+++":"+finalURL);
                        download(finalURL,tempPath);
                        sop("下载成功");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        sop("下载完毕");
        delMultyFile(downloadPath);
        sop("已经删除所有空图");
    }
    public static void delMultyFile(String path){
        File file = new File(path);
        if(!file.exists())
        { throw new RuntimeException("File \""+path+"\" NotFound when excute the method of delMultyFile()....");}
        File[] fileList = file.listFiles();
        File tempFile=null;
        for(File f : fileList){
            if(f.isDirectory()){
                { delMultyFile(f.getAbsolutePath());}
            }else{
                if(f.length()==0)
                { sop(f.delete()+"---"+f.getName());}
            }
        }
    }
    public static List<String> nameList(String nameList){
        List<String> arr = new ArrayList<String>();
        String[] list;
        if(nameList.contains(","))
        { list= nameList.split(",");}
        else if(nameList.contains("、"))
        { list= nameList.split("、");}
        else if(nameList.contains(" "))
        {list= nameList.split(" ");}
        else{
            arr.add(nameList);
            return arr;
        }
        for(String s : list){
            arr.add(s);
        }
        return arr;
    }
    public static void sop(Object obj){
        System.out.println(obj);
    }
    //根据图片网络地址下载图片
    public static void download(String url,String path){
        //path = path.substring(0,path.length()-2);
        File file= null;
        File dirFile=null;
        FileOutputStream fos=null;
        HttpURLConnection httpCon = null;
        URLConnection con = null;
        URL urlObj=null;
        InputStream in =null;
        byte[] size = new byte[1024];
        int num=0;
        try {
            String downloadName= url.substring(url.lastIndexOf("/")+1);
            dirFile = new File(path);
            if(!dirFile.exists() && path.length()>0){
                if(dirFile.mkdir()){
                    sop("creat document file \""+path.substring(0,path.length()-1)+"\" success...\n");
                }
            }else{
                file = new File(path+downloadName);
                fos = new FileOutputStream(file);
                if(url.startsWith("http")){
                    urlObj = new URL(url);
                    con = urlObj.openConnection();
                    httpCon =(HttpURLConnection) con;
                    in = httpCon.getInputStream();
                    while((num=in.read(size)) != -1){
                        for(int i=0;i<num;i++)
                        {  fos.write(size[i]);}
                    }
                }
            }
        }catch (FileNotFoundException notFoundE) {
            sop("找不到该网络图片....");
        }catch(NullPointerException nullPointerE){
            sop("找不到该网络图片....");
        }catch(IOException ioE){
            sop("产生IO异常.....");
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
Java 中编写程序爬取图片通常涉及到网络请求、HTML 解析以及文件操作等技术。下面是一个简单的步骤概览,帮助您了解如何从网页上下载图片: ### 1. 使用库 首先,您需要引入合适的库来进行 HTTP 请求和解析 HTML 结构。常用的库包括: - **Jsoup**:用于解析 HTML 和 XML 文件。 - **HttpClient 或 Apache HttpClient**:用于发送 HTTP 请求。 ### 2. 获取网页内容 使用库发起 GET 请求到目标 URL,并获取返回的网页内容。例如,使用 Jsoup 的基本示例: ```java import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class WebScraper { public static void main(String[] args) throws Exception { String url = "https://example.com/images"; Document doc = Jsoup.connect(url).get(); // 找到所有 img 标签 Elements images = doc.select("img"); for (Element image : images) { System.out.println(image.absUrl("src")); // 输出图片链接 } } } ``` ### 3. 下载图片 从 HTML 结果中提取图片链接后,可以使用 `HttpClient` 发送另一个请求来实际下载图片: ```java import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.FileOutputStream; import java.io.IOException; public class ImageDownloader { public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://example.com/image.jpg"); HttpResponse response = httpClient.execute(httpGet); try (FileOutputStream fos = new FileOutputStream("image.jpg")) { fos.write(EntityUtils.toByteArray(response.getEntity())); } } } ``` ### 相关问题: 1. **如何处理网页结构变化**? 网页的 HTML 结构可能会随时间而改变,因此需要动态地定位图片元素并适应新的布局。这可能需要定期更新查找规则,或者采用更高级的技术如 XPath 来定位节点。 2. **错误处理**? 上面的例子没有包含异常处理。在真实应用中,应该添加适当的异常处理机制,比如捕获网络请求失败或文件写入错误等。 3. **性能优化**? 对于大量图片的抓取,应考虑使用并发请求提高效率,同时对网络连接进行管理避免过快的请求导致服务器拒绝服务。可以使用线程池或异步I/O技术来优化性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隐姓埋名lx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值