zoukankan      html  css  js  c++  java
  • webmagic爬虫抓取工作室成员博客

    一、导入依赖

     <!--webmagic依赖-->
    <dependency>
        <groupId>us.codecraft</groupId>
        <artifactId>webmagic-core</artifactId>
        <version>0.7.3</version>
    </dependency>
    <dependency>
        <groupId>us.codecraft</groupId>
        <artifactId>webmagic-extension</artifactId>
        <version>0.7.3</version>
    </dependency>

    二、参考网站

    适用博客:博客园、CSDN博客、简书

    框架参考文档:http://webmagic.io/docs/zh/

    X Path语法:http://www.w3school.com.cn/xpath/xpath_syntax.asp

    三、核心代码

      1 import us.codecraft.webmagic.Page;
      2 import us.codecraft.webmagic.Site;
      3 import us.codecraft.webmagic.Spider;
      4 import us.codecraft.webmagic.model.OOSpider;
      5 import us.codecraft.webmagic.processor.PageProcessor;
      6 
      7 import java.io.IOException;
      8 import java.text.SimpleDateFormat;
      9 import java.util.ArrayList;
     10 import java.util.List;
     11 import java.util.Map;
     12 
     13 /**
     14  * @author zsh
     15  * @site qqzsh.top
     16  * @company wlgzs
     17  * @create 2019-04-08 20:59
     18  * @Description
     19  */
     20 public class CnBlogProcessor implements PageProcessor {
     21 
     22     // 抓取网站的相关配置,包括:编码、抓取间隔、重试次数等
     23     private Site site = Site.me().setCharset("utf-8").setRetryTimes(5).setSleepTime(1000);
     24     // 文章数量
     25     private static int size = 0;
     26     // 文章集合
     27     private static List<CnBlogs> cnBlogses = new ArrayList<>();
     28 
     29     // 抽取逻辑类
     30     public void process(Page page) {
     31         CnBlogs cnBlogs = new CnBlogs();
     32         //博客园博客
     33         if (page.getUrl().regex("https://www.cnblogs.com/.*").match()) {
     34             // 标题
     35             //暂时发现3个样式
     36             String title = page.getHtml().xpath("//div[@class='entrylistPosttitle']/a/text()").get();
     37             if (title != null){
     38                 cnBlogs.setTitle(title);
     39             }else {
     40                 title = page.getHtml().xpath("//div[@class='postTitle']/a/text()").get();
     41                 if (title != null){
     42                     cnBlogs.setTitle(title);
     43                 }else {
     44                     title =  page.getHtml().xpath("//div[@class='post']/h5/a/text()").get();
     45                     cnBlogs.setTitle(title);
     46                 }
     47             }
     48 
     49             // 作者
     50             cnBlogs.setAuthor(page.getHtml().xpath("//a[@id='Header1_HeaderTitle']/text()").get());
     51 
     52             // 发布日期
     53             String datatime = page.getHtml().xpath("//div[@class='entrylistItemPostDesc']/a/text()").get();
     54             if (datatime != null){
     55                 cnBlogs.setDateTime(datatime);
     56             }else {
     57                 datatime = page.getHtml().xpath("//div[@class='postDesc']/text()").get();
     58                 if (datatime != null){
     59                     int qian = datatime.indexOf("@");
     60                     int hou = datatime.indexOf(cnBlogs.getAuthor());
     61                     datatime = datatime.substring(qian+2,hou-1);
     62                     cnBlogs.setDateTime(datatime);
     63                 }else {
     64                     datatime = page.getHtml().xpath("//p[@class='postfoot']/a/text()").get();
     65                     cnBlogs.setDateTime(datatime);
     66                 }
     67             }
     68             // URL
     69             String url = page.getHtml().xpath("//div[@class='entrylistPosttitle']/a/@href").get();
     70             if (url != null){
     71                 cnBlogs.setUrl(url);
     72             }else {
     73                 url = page.getHtml().xpath("//div[@class='postTitle']/a/@href").get();
     74                 if (url != null){
     75                     cnBlogs.setUrl(url);
     76                 }else {
     77                     url =  page.getHtml().xpath("//div[@class='post']/h5/a/@href").get();
     78                     cnBlogs.setUrl(url);
     79                 }
     80             }
     81             cnBlogses.add(cnBlogs);
     82         }else if (page.getUrl().regex("https://blog.csdn.net/.*").match()){  //csdn博客
     83             // 标题
     84             String title = page.getHtml().xpath("//div[@class='article-item-box csdn-tracking-statistics']/h4/a/text()").all().get(1);
     85             if (title != null){
     86                 cnBlogs.setTitle(title);
     87             }
     88             // 作者
     89             cnBlogs.setAuthor(page.getHtml().xpath("//a[@id='uid']/text()").get());
     90 
     91             // 发布日期
     92             String datatime = page.getHtml().xpath("//span[@class='date']/text()").all().get(1);
     93             if (datatime != null){
     94                 cnBlogs.setDateTime(datatime);
     95             }
     96 
     97             // URL
     98             String url = page.getHtml().xpath("//div[@class='article-item-box csdn-tracking-statistics']/h4/a/@href").all().get(1);
     99             if (url != null){
    100                 cnBlogs.setUrl(url);
    101             }
    102             cnBlogses.add(cnBlogs);
    103         }else if (page.getUrl().regex("https://www.jianshu.com/.*").match()){ //简书
    104             // 标题
    105             String title = page.getHtml().xpath("//div[@class='content']/a/text()").get();
    106             if (title != null){
    107                 cnBlogs.setTitle(title);
    108             }
    109             // 作者
    110             cnBlogs.setAuthor(page.getHtml().xpath("//a[@class='name']/text()").all().get(1));
    111 
    112             // 发布日期
    113             String datatime = page.getHtml().xpath("//span[@class='time']/@data-shared-at").get();
    114             if (datatime != null){
    115                 cnBlogs.setDateTime(datatime);
    116             }
    117 
    118             // URL
    119             String url = "https://www.jianshu.com"+page.getHtml().xpath("//div[@class='content']/a/@href").get();
    120             if (url != null){
    121                 cnBlogs.setUrl(url);
    122             }
    123             cnBlogses.add(cnBlogs);
    124 
    125         }else {
    126             cnBlogses.add(cnBlogs);
    127         }
    128     }
    129 
    130     public Site getSite() {
    131         return site;
    132     }
    133 
    134     public static void main(String[] args) throws IOException {
    135         //1.先去小组官网抓取人员名单
    136         Spider.create(new Test()).addUrl("http://wlgzs.org/blog.html").thread(5).run();
    137         //2.根据抓取的名单来获取博客
    138         Map<String, String> map = POITest.readExcel();
    139         List<String> name = new ArrayList<>();
    140         List<String> url = new ArrayList<>();
    141         for (Map.Entry<String, String> entry : map.entrySet()) {
    142             //Map.entry<Integer,String> 映射项(键-值对)  有几个方法:用上面的名字entry
    143             //entry.getKey() ;entry.getValue(); entry.setValue();
    144             //map.entrySet()  返回此映射中包含的映射关系的 Set视图。
    145             System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    146             name.add(entry.getKey());
    147             url.add(entry.getValue());
    148             if (!entry.getKey().equals("计科182 杨惠涵")){
    149                 Spider.create(new CnBlogProcessor()).addUrl(entry.getValue()).thread(10).run();
    150             }else {
    151                 CnBlogProcessor.cnBlogses.add(new CnBlogs());
    152             }
    153         }
    154         POITest.printExcel2(name,url,CnBlogProcessor.cnBlogses);
    155     }
    156 }

    四、项目源码地址

    https://gitee.com/ZhangShunHai/webmagic

    result.xls文件是从工作室官网抓取的成员名单

    result2.xls是抓取的最近一篇博客的链接

  • 相关阅读:
    ASP.NET Zero--10.一个例子(3)商品分类管理-新建
    ASP.NET Zero--9.一个例子(2)商品分类管理-列表
    ASP.NET Zero--8.一个例子(1)菜单添加
    ASP.NET Zero--7.控制器加权限
    ASP.NET Zero--6.菜单加权限
    ASP.NET Zero--5.配置权限
    ASP.NET Zero--4.不使用谷歌字体,提升加载速度
    ASP.NET Zero--2.如何启动
    C# mongoDB Driver 使用对象方式最完善查询语法大全
    ef 数据库连接字符串加密
  • 原文地址:https://www.cnblogs.com/zsh-blogs/p/10679864.html
Copyright © 2011-2022 走看看