吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2659|回复: 4
收起左侧

[Java 转载] webcollector框架爬虫古装汉服图片

[复制链接]
qq58452077 发表于 2019-8-12 14:51
本帖最后由 qq58452077 于 2019-8-12 14:57 编辑

import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;
import cn.edu.hfut.dmic.webcollector.util.ExceptionUtils;
import cn.edu.hfut.dmic.webcollector.util.FileUtils;
import cn.edu.hfut.dmic.webcollector.util.MD5Utils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Crawling news from hfut news
 *
 * home.php?mod=space&uid=686208 hu
 */
public class GuZhuangCrawler extends BreadthCrawler {
    /**
     * home.php?mod=space&uid=952169 crawlPath crawlPath is the path of the directory which maintains
     *                  information of this crawler
     * @param autoParse if autoParse is true,BreadthCrawler will auto extract
     *                  links which match regex rules from pag
     */
    File baseDir = new File("E:\\home");

    public GuZhuangCrawler(String crawlPath, boolean autoParse) {
        super(crawlPath, autoParse);
        setThreads(50);
        getConf().setExecuteInterval(5000);
        getConf().setConnectTimeout(0);
        getConf().setReadTimeout(0);
        getConf().setTopN(100);
        //getConf().getDefaultUserAgent();
        //setResumable(true);
    }

    public void visit(Page page, CrawlDatums next) {
        String url = page.url();
        /*if page is news page*/
        if (page.matchType("list")) {
            /*extract title and content of news by css selector*/
            Elements eList = page.select("div#threadlist > div.sk-box > div.bd > div.sk-vlist > div");
            for (Element element : eList) {
                Element link = element.select("div.v-link > a").first();
                if(link!=null){
                    String title = link.attr("title");
                    String href = link.attr("href");
                    System.out.println("URL:" + url);
                    System.out.println("title:" + title);
                    System.out.println("href:" + href);
                    next.addAndReturn(href).type("img").meta("title", title);
                }
            }
        }
        if (page.matchType("img")) {
            String title  = page.meta("title");
            String regex = "[\u4e00-\u9fa5]+";
            Matcher m = Pattern.compile(regex).matcher(title);
            StringBuffer str = new StringBuffer();
            while (m.find()){
                str.append(m.group());
            }
            title = str.toString();
            Elements eList = page.select("div#postlist > div");
            for(int i=0;i<eList.size()-1;i++){
                Elements links = null;
                if(i==0){
                    if(CollectionUtils.isEmpty(links)){
                        links = eList.get(i).select("div#jiathis_share_CODE_HTML4 > div.t_fsz >table > tbody > tr:nth-child(1) >td.t_f >ignore_js_op");
                    }
                    if(CollectionUtils.isEmpty(links)){
                        links = eList.get(i).select("#jiathis_share_CODE_HTML4 > div.t_fsz >table > tr:nth-child(1) >td.t_f >div:nth-last-of-type(1) > font > font > font > ignore_js_op");
                    }
                    if(CollectionUtils.isEmpty(links)){
                        links = eList.get(i).select("#jiathis_share_CODE_HTML4 > div.t_fsz >table > tr:nth-child(1) >td.t_f > ignore_js_op");
                    }
                    if(CollectionUtils.isEmpty(links)){
                        links = eList.get(i).select("table.plhin > tbody > tr:nth-child(1) > td.plc > div.pct > div.pcb > div.t_fsz > table > tbody > tr:nth-child(1) > td.t_f > div > ignore_js_op");
                    }

                }else {
                    if(CollectionUtils.isEmpty(links)){
                        links = eList.get(i).select("table.plhin > tbody > tr:nth-child(1) > td.plc > div.pct > div.pcb > div.t_fsz > table > tbody > tr:nth-child(1) >td.t_f > ignore_js_op");
                    }
                    if(CollectionUtils.isEmpty(links)){
                        links = eList.get(i).select("table.plhin > tbody > tr:nth-child(1) > td.plc > div.pct > div.pcb > div.t_fsz > table > tbody > tr:nth-child(1) > td.t_f > div > ignore_js_op");
                    }

                }
                for(Element e:links){
                    Element link = e.select("img").first();
                    String src = link.attr("zoomfile");
                    String aid = link.attr("aid");
                    System.out.println("title:\n" + title);
                    System.out.println("content:\n" + src);
                    next.addAndReturn("http://www.52guzhuang.com/"+src).type("downloadImg").meta("title", title).meta("name",aid);
                }
                //#jiathis_share_CODE_HTML4
                //Element link = eList.get(i).select("table.plhin > tbody > tr:nth-child(1) > td.plc > div.pct > div.pcb > div.t_fsz > table > tbody > tr:nth-child(1) >td.t_f > ignore_js_op:nth-child(1) >img").first();

            }
            Element ele = page.select("div#ct > div.pgs.mtm.mbm.cl > div.pg > a:nth-last-child(3)").first();
            //Element ele1 = page.select("div#ct > div.pgs.mtm.mbm.cl > div.pg > a").get(-2);
            if(null!=ele){
                String pageNum = ele.text();
                if(null!=pageNum&& Integer.valueOf(pageNum)>1){
                    String subStr = url.substring(0,url.lastIndexOf("-") -1);
                    for (int i = 2; i<=Integer.valueOf(pageNum); i++) {
                        next.addAndReturn(subStr+i+"-1.html").type("img").meta("title", title);
                    }
                }
            }
        }
        if (page.matchType("downloadImg")){
            String title  = page.meta("title");
            //根据http头中的Content-Type信息来判断当前资源是网页还是图片
            String contentType = page.contentType();
            //根据Content-Type判断是否为图片
            if(contentType!=null && contentType.startsWith("image")){
                //从Content-Type中获取图片扩展名
                String extensionName=contentType.split("/")[1];
                try {
                    byte[] image = page.content();
                    //根据图片MD5生成文件名
                    String fileName = String.format("%s.%s", MD5Utils.md5(image), extensionName);
                    File imageFile = new File(baseDir.getPath()+"\\"+title,fileName);
                    FileUtils.write(imageFile,image);
                    System.out.println("保存图片 "+page.url()+" 到 "+ imageFile.getAbsolutePath());
                } catch (Exception e) {
                    ExceptionUtils.fail(e);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        GuZhuangCrawler crawler = new GuZhuangCrawler("crawl", true);
        /*可以设置每个线程visit的间隔,这里是毫秒*/
        //crawler.setVisitInterval(1000);
        /*可以设置http请求重试的间隔,这里是毫秒*/
        //crawler.setRetryInterval(1000);

        for (int i = 1; i <= 1; i++) {
            String seedUrl = "http://www.52guzhuang.com/forum-59-"+i+".html";
            crawler.addSeedAndReturn(seedUrl).meta("pageNum", i).type("list");
        }
        crawler.start(6);
    }
}


汉服小姐姐

汉服小姐姐


使用教程:
1.pom.xml文件添加依赖
<dependencies>
        <dependency>
            <groupId>cn.edu.hfut.dmic.webcollector</groupId>
            <artifactId>WebCollector</artifactId>
            <version>2.71</version>
        </dependency>
    </dependencies>
2.https://github.com/CrawlScript/WebCollector

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

maplefly 发表于 2019-8-12 15:50
借鉴一下写个 C#版的
xincarry 发表于 2019-8-12 15:58
不搞这个,但也来支持下吧,毕竟这两年慢慢流行了
Bii 发表于 2019-8-21 14:51
niebaohua 发表于 2019-8-22 11:32
图片好评,不错
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-20 00:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表