WebMagic

原文链接: CSDN@qq_44885775#WebMagic

WebMagic官网:Introduction · WebMagic Documents

Spider

原文链接:Introduction · WebMagic Documents

方法 说明 示例
create(PageProcessor) 创建Spider Spider.create(new GithubRepoProcessor())
addUrl(String…) 添加初始的URL spider.addUrl("Choose a language · WebMagic Documents")
addRequest(Request...) 添加初始的Request spider.addRequest("Choose a language · WebMagic Documents")
thread(n) 开启n个线程 spider.thread(5)
run() 启动,会阻塞当前线程执行 spider.run()
start()/runAsync() 异步启动,当前线程继续执行 spider.start()
stop() 停止爬虫 spider.stop()
test(String) 抓取一个页面进行测试 spider.test("Choose a language · WebMagic Documents")
addPipeline(Pipeline) 添加一个Pipeline,一个Spider可以有多个Pipeline spider.addPipeline(new ConsolePipeline())
setScheduler(Scheduler) 设置Scheduler,一个Spider只能有个一个Scheduler spider.setScheduler(new RedisScheduler())
setDownloader(Downloader) 设置Downloader,一个Spider只能有个一个Downloader spider.setDownloader(new SeleniumDownloader())
get(String) 同步调用,并直接取得结果 ResultItems result = spider.get("Choose a language · WebMagic Documents")
getAll(String…) 同步调用,并直接取得一堆结果 List results = spider.getAll("Choose a language · WebMagic Documents", "http://webmagic.io/xxx")

Site

对站点本身的一些配置信息,例如编码、HTTP头、超时时间、重试策略等、代理等,都可以通过设置Site对象来进行配置。

方法 说明 示例
setCharset(String) 设置编码 site.setCharset("utf-8")
setUserAgent(String) 设置UserAgent site.setUserAgent("Spider")
setTimeOut(int) 设置超时时间,单位是毫秒 site.setTimeOut(3000)
setRetryTimes(int) 设置重试次数 site.setRetryTimes(3)
setCycleRetryTimes(int) 设置循环重试次数 site.setCycleRetryTimes(3)
addCookie(String,String) 添加一条cookie site.addCookie("dotcomt_user","code4craft")
setDomain(String) 设置域名,需设置域名后,addCookie才可生效 site.setDomain("github.com")
addHeader(String,String) 添加一条addHeader site.addHeader("Referer","https://github.com")
setHttpProxy(HttpHost) 设置Http代理 site.setHttpProxy(new HttpHost("127.0.0.1",8080))

其中循环重试cycleRetry是0.3.0版本加入的机制。

该机制会将下载失败的url重新放入队列尾部重试,直到达到重试次数,以保证不因为某些网络原因漏抓页面。


从0.4.0版本开始,WebMagic开始支持Http代理。因为场景的多样性,代理这部分API一直处于不稳定状态,但是因为需求确实存在,所以WebMagic会继续支持代理部分的完善。目前发布的API只是beta版,后续API可能会有更改。代理相关的设置都在Site类中。

API 说明
Site.setHttpProxy(HttpHost httpProxy) 设置单一的普通HTTP代理
Site.setUsernamePasswordCredentials(UsernamePasswordCredentials usernamePasswordCredentials) 为HttpProxy设置账号密码
Site.setHttpProxyPool(List\ httpProxyList, boolean isUseLastProxy) 设置代理池

设置单一的普通HTTP代理

101.101.101.101的8888端口,并设置密码为"username","password"

site.setHttpProxy(new HttpHost("101.101.101.101",8888))
    .setUsernamePasswordCredentials(new UsernamePasswordCredentials("username","password"))

设置代理池

101.101.101.101和102.102.102.102两个IP

site.setHttpProxyList输入是IP+PORT, isUseLastProxy是指重启时是否使用上一次的代理配置

List<String[]> poolHosts = new ArrayList<String[]>();
poolHosts.add(new String[]{"username","password","101.101.101.101","8888"});
poolHosts.add(new String[]{"username","password","102.102.102.102","8888"});
site.setHttpProxyPool(poolHosts,false);httpProxyList输入是IP+PORT, isUseLastProxy是指重启时是否使用上一次的代理配置

0.6.0版本后,允许实现自己的代理池,通过扩展接口ProxyPool来实现。目前WebMagic的代理池逻辑是:轮流使用代理池中的IP,如果某个IP失败超过20次则增加2小时的重用时间,具体实现可以参考SimpleProxyPool。


配置代理 · WebMagic Documents

从0.7.1版本开始,WebMagic开始使用了新的代理APIProxyProvider。因为相对于Site的“配置”,ProxyProvider定位更多是一个“组件”,所以代理不再从Site设置,而是由HttpClientDownloader设置。

API 说明
HttpClientDownloader.setProxyProvider(ProxyProvider proxyProvider) 设置代理

设置单一的普通HTTP代理为101.101.101.101的8888端口,并设置密码为"username","password"

HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
httpClientDownloader.setProxyProvider(SimpleProxyProvider.from(new Proxy("101.101.101.101",8888,"username","password")));
spider.setDownloader(httpClientDownloader);

设置代理池,其中包括101.101.101.101和102.102.102.102两个IP,没有密码

HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
httpClientDownloader.setProxyProvider(SimpleProxyProvider.from(new Proxy("101.101.101.101",8888),new Proxy("102.102.102.102",8888)));

爬虫监控

爬虫的监控 · WebMagic Documents

windows下是在DOS下输入jconsole.exe


Java爬虫框架WebMagic的介绍及使用(定时任务、代理)_Piconjo_Official的博客-CSDN博客_webmagic定时任务

CSS选择器抽取

page.putField("ul",page.getHtml().css("div.right-1 ul").all());
 
page.putField("ul",page.getHtml().$("div.right-1 ul").all());

css()等价于$()

// 实现PageProcessor类 复写方法
public class JobProcessor implements PageProcessor {
 
    // 解析页面
    public void process(Page page)
    {
        将结果以键值对的形式放入ResultItems中
        page.putField("ul",page.getHtml().css("div.right-1 ul").all());
    }
 
    private Site site=Site.me();
    public Site getSite()
    {
        return site;
    }
 
    // 主函数 执行爬虫
    public static void main(String[] args)
    {
        // Spider容器创建解析器 添加url地址 run执行爬虫
        Spider.create(new JobProcessor()).addUrl("http://www.zjitc.net/xwzx/tztg.htm").run();
    }
}

获取元素

一条抽取规则 无论是XPath CSS选择器或是正则表达式 有可能抽取到多条元素
WebMagic可以通过不同的API获取到一个或多个元素
返回一条String类型的结果:

        (默认返回第一条)

        get()
        例:String link=html.links().get()

        toString()
        例:String link=html.links().toString()

返回所有抽取结果:

        all()
        例:List links=html.links().all()

        // 将class为right-1的div中的ul中的所有a标签作为目标链接  
page.addTargetRequests(page.getHtml().css("div.right-1 ul a").links().all());                               

Site爬虫配置

Site.me()可对爬虫进行一些配置 包括编码字符 抓取间隔 超时时间 重试次数等

private Site site=Site.me()
            .setCharset("utf8") //设置编码
            .setTimeOut(10000) //设置超时时间(单位:毫秒)
            .setRetryTimes(3000) //设置重试的时间间隔(单位:毫秒)
            .setSleepTime(3); //设置重试次数
            
public Site getSite() {
        return site;
}

其它设置:

setUserAgent(String):设置代理
addCookie(String):添加Cookie
setDomain(String):设置域名
addHeader(String,String):添加请求头
setHttpProxy(HttpHost):设置Http代理

返回Site

public class Xxxxxxxx  implements PageProcessor {
 
 
    private Site site=Site.me()
            .setCharset("utf8") //设置编码
            .setTimeOut(10000) //设置超时时间(单位:毫秒)
            .setRetryTimes(3000) //设置重试的时间间隔(单位:毫秒)
            .setSleepTime(3); //设置重试次数
 
 
   
    private Site site=Site.me();
    @Override
    public Site getSite() {
        return site;
    }
 
}

创建代理的爬虫

@Scheduled(fixedDelay = 1000)
public void Process()
{
 	// 创建下载器Downloader
    HttpClientDownloader httpClientDownloader=new HttpClientDownloader();
 
    // 给下载器设置代理服务器信息
    httpClientDownloader.setProxyProvider(SimpleProxyProvider.from(new Proxy("183.91.33.41",89)));
 
    Spider.create(new ProxyTest())
            .addUrl("http://ip.chinaz.com/")
            // 设置下载器
            .setDownloader(httpClientDownloader)
            .run();
}
posted @ 2023-02-27 15:49  itwetouch  阅读(181)  评论(0编辑  收藏  举报