栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

webmagic 爬取网站图片

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

webmagic 爬取网站图片

注意:这是通用写法,不同的网站获取的方式可能有所不同,可以在此基础上修改。

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

public class baseCrawling implements PageProcessor {
    private Site site = Site.me().setCharset("utf8").setRetryTimes(1000).setSleepTime(1000);
    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        System.out.println("html:"+html);
        List list =page.getHtml().$("img","src").regex(".*https.*").all();
        for(String str : list){
            System.out.println("str:"+str);
            downLoadFileToPath(str,"D://tupian");
        }

        List urlList = page.getHtml().$("a","href").regex(".*https.*").all();
        for(String url : urlList){
            baseCrawling my = new baseCrawling();
            Spider.create(my).addUrl(url).thread(5).run();
            System.out.println("url:"+url);
        }
    }
    @Override
    public Site getSite() {
        return site;
    }


    
    private static void downLoadFileToPath(String urlStr,String savePath) {
        URL url = null;
        InputStream inputStream = null;
        FileOutputStream fos = null;
        try {
            url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

            //得到输入流
            inputStream = conn.getInputStream();
            //获取自己数组
            byte[] getData = readInputStream(inputStream);

            //文件保存位置
            File saveDir = new File(savePath);
            if(!saveDir.exists()){
                saveDir.mkdir();
            }
            String str="";
            String date =String.valueOf(new Date().getTime());
            if(urlStr.contains("?")){
                int strStartIndex = urlStr.lastIndexOf(".");
                int strEndIndex = urlStr.indexOf("?");
                String result = urlStr.substring(strStartIndex, strEndIndex).substring(".".length());

                str = date+"."+result;
            }else{
                str = date +"."+urlStr.substring(urlStr.lastIndexOf(".")+1);
            }

            File file = new File(saveDir+File.separator+str);

            fos = new FileOutputStream(file);
            fos.write(getData);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(fos!=null) {
                    fos.close();
                }
                if(inputStream!=null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("url:"+url+" download success");
    }
    private static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
    public static void main(String[] args) {
        baseCrawling my = new baseCrawling();
        System.out.println("开始爬取");
        Spider.create(my).addUrl("https://www.tupianzj.com/meinv/").thread(5).run();
        System.out.println("爬取结束");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号