当前位置: 移动技术网 > IT编程>开发语言>Java > 签名图片一键批量生成 使用Java的Webmagic爬虫实现

签名图片一键批量生成 使用Java的Webmagic爬虫实现

2020年01月12日  | 移动技术网IT编程  | 我要评论

使用webmagic爬虫实现的签名档一键生成

实现原理

这里爬取的网址是http://jiqie.zhenbi.com/c/
然后获取到里面提交数据,提交地址,在对这些数据进行post提交
解析html标签获得图片地址并输出到控制台

不会使用webmagic爬虫框架的 自行百度配置
本文主要是学习post提交

测试结果:

下面请看代码实现

import us.codecraft.webmagic.page;
import us.codecraft.webmagic.request;
import us.codecraft.webmagic.site;
import us.codecraft.webmagic.spider;
import us.codecraft.webmagic.model.httprequestbody;
import us.codecraft.webmagic.processor.pageprocessor;
import us.codecraft.webmagic.utils.httpconstant;

import java.util.hashmap;

import java.util.list;
import java.util.map;
import java.util.random;
import java.util.regex.matcher;
import java.util.regex.pattern;

public class postdemo implements pageprocessor {
    site site = site.me().setretrytimes(3).settimeout(1500);
    @override
    public void process(page page) {
        random r = new random();

        //生成的文字
        string text = "快乐的小蜘蛛";

        //第一次请求:获取所有类型
        if ("http://jiqie.zhenbi.com/c/".equals(page.geturl().tostring())){



            //解析a标签
            //<a href="34.htm">彩色文字版本墨人动态签名档在线制作</a>

            list<string> list = page.gethtml().$("a","href").all();
            list<string> listtext = page.gethtml().$("a","text").all();

            // 创建 pattern 对象 匹配数字
            pattern r = pattern.compile("[0-9]+");

            // 现在创建 matcher 对象
            matcher m;
            request req;
            for (int i =0;i<list.size();i++){
                m = r.matcher(list.get(i));
                if (!m.find())continue;
                req = new request();
                req.seturl("http://jiqie.zhenbi.com/c/"+ m.group()+".htm");
                req.putextra("mindex",m.group());
                req.putextra("name",listtext.get(i));
                page.addtargetrequest(req);
            }
            return;

        //第二次请求:获取数据参数以及post提交地址
        }else if(page.geturl().tostring().endswith(".htm")){
            //是否有id :show
            if (!page.gethtml().$("#show").match())return;

            request request =page.getrequest();
            request.setmethod(httpconstant.method.post);

            //zhenbi('re2.php','0');
            string index = page.gethtml().$("#up","onclick").regex("\\w+\\.").tostring().replace(".","");
            system.out.println(index);

            request.seturl("http://jiqie.zhenbi.com/c/"+index+".php");

            map<string,object> map = new hashmap<>();

            /*   提交数据

            id  我是zht0301 我为自己代言
            idi jiqie
            id1 20
            id2 16
            id3 26
            id4
            id5 #624475
            id6
             */

            //获取参数

            map.put("id",text);
            map.put("idi","jiqie");

            map.put("id1",page.gethtml()
                    .$("#id1").xpath("//select/option[@selected='selected']")
                    .$("option","value"));

            map.put("id2",page.gethtml()
                    .$("#id2").xpath("//select/option[@selected='selected']")
                    .$("option","value"));

            map.put("id3",page.gethtml().$("#id3","value"));
            map.put("id4",page.gethtml().$("#id4","value"));
            map.put("id5",page.gethtml().$("#id5","value"));
            map.put("id6",page.gethtml().$("#id6","value"));


            system.out.println(map);
            request.setrequestbody(httprequestbody.form(map,"utf-8"));
            page.addtargetrequest(request);
            try {
                //随缘冷静,冲动是魔鬼
                thread.sleep(r.nextint(500)+100);
            } catch (interruptedexception e) {
                e.printstacktrace();
            }

        //第三次请求:获取图片的地址
        }else{
            page.putfield("id",page.getrequest().getextra("mindex"));
            page.putfield("name",page.getrequest().getextra("name"));
            page.putfield("img_src",page.gethtml().$("img","src"));
        }
    }

    @override
    public site getsite() {
        return site;
    }


    public static void main(string[] args) {

        spider.create(new postdemo())
                .addurl("http://jiqie.zhenbi.com/c/")
                .thread(1)
                .run();
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网