webMagic入门案例,启动流程,及从页面中获取元素,并放到Page对象中,并成功拿出
本例作用:
1)首次成功配置,了解软件运行机制
2)从页面中获取元素三种方式 jsoup,xpath,css
3) page.putField("subject","尝试存入结果项的内容,可从页面中获取值");
4)page.getResultItems().get("subject") 从结果集中拿出数据
5)List<String> links =page.getHtml().links().all(); 取出所有超链接
6)通过正则表达式找出结果集,放到到page中,并取出打印foreach()
//找出所有链接下详细招聘格式网址 http://sqrcw.com/zhaopin/job_233460.html List<String> linkDetails =page.getHtml().links().regex("(^http://sqrcw.com\\/zhaopin\\/job_\\d*\\.html)").all(); page.putField("linkDetails",linkDetails); //放入到Page对象中 List<ResultItems> linkDetails_inPage = page.getResultItems().get("linkDetails");
完整第一天学习代码:
package com.ibaiqi.study; import us.codecraft.webmagic.Page; import us.codecraft.webmagic.ResultItems; import us.codecraft.webmagic.Site; import us.codecraft.webmagic.Spider; import us.codecraft.webmagic.pipeline.JsonFilePipeline; import us.codecraft.webmagic.processor.PageProcessor; import us.codecraft.webmagic.selector.Selectable; import java.util.List; import java.util.Map; /** * 快速入门,了解webMagic启动流程,及从页面中选择元素 * 同时以www.sqrcw.com获取详细招聘信息 */ public class Sqrcw_comProcessor implements PageProcessor { Site site=Site.me() .addCookie("userName2","zhangdaxu") .setRetryTimes(3).setSleepTime(100).setTimeOut(20000); @Override public void process(Page page) { System.out.println(site); System.out.println("开始采集"+page.getUrl()); System.out.println("开始采集超链接"+page.getHtml().css(".s1 a")); //尝试向结果项(采集存入项)中写入数据 page.putField("subject","尝试存入结果项的内容"); //测试从结果项中拿出数据 System.out.println("取结果项值:"+page.getResultItems().get("subject")); page.putField("职位",page.getHtml().css(".s1 a")); //得到结果项,返回ResultItems System.out.println(""+page.getResultItems()); System.out.println(page.getResultItems().get("subject")); //返回json的结果为json System.out.println(page.getJson()); //获得当前页面超链接 System.out.println("page.getUrl()"); System.out.println(page.getUrl().all()); System.out.println("selectAble() 从站点中查找部份内容"); Selectable selectable=page.getHtml(); System.out.println(selectable.$("#main1")); String url = page.getRequest().getUrl(); System.out.println("url"+url); Map<String, String> cookies = page.getRequest().getCookies(); // System.out.println("cookies"+cookies); // System.out.println(page.getCharset()); // System.out.println(site.getCookies()); // System.out.println(page.getStatusCode()); System.out.println("得到起链接总数:"+page.getHtml().links().all().size()); System.out.println("得到所有超链接:"); List<String> links =page.getHtml().links().all(); int i=0; for (String link:links ) { i++; System.out.println(i+"条:"+link); } System.out.println("正则表达式,找出招聘详细页面"); //找出所有链接下详细招聘格式网址 http://sqrcw.com/zhaopin/job_233460.html List<String> linkDetails =page.getHtml().links().regex("(^http://sqrcw.com\\/zhaopin\\/job_\\d*\\.html)").all(); for (String linkDetail: linkDetails) { System.out.println(linkDetail); } System.out.println("详细页面总共:"+linkDetails.size()); //放到page对象中,并取出,目的是为以后放进pipeline对象中,做持久化操作 page.putField("linkDetails",linkDetails); //放入到Page对象中 List<ResultItems> linkDetails_inPage = page.getResultItems().get("linkDetails"); System.out.println("page对象中取出的数据"); for (Integer j=0 ;j<linkDetails_inPage.size();j++){ System.out.println(linkDetails_inPage.get(j)); } } @Override public Site getSite() { return site; } public static void main(String[] args) { System.out.println("开始采集001"); //本类是实现 pageProcessor PageProcessor pageProcessor = new Sqrcw_comProcessor(); //从才网查找数据详细页面测试 Spider.create(pageProcessor).addUrl("http://sqrcw.com/job/") .addPipeline(new JsonFilePipeline("D:\\data\\webmagic")) .run(); System.out.println("采集结束0001"); } }
做产品的程序,才是好的程序员!