java 爬虫学习 笔记一 使用爬虫框架 WebMagic

1.WebMagic 官方文档地址  http://webmagic.io/docs/zh/

引入WebMagic的jar 这里采用pom形式

 <!-- 使用webmagic所用的jar -->
  <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>
 
 <dependency>
    <groupId>us.codecraft</groupId>
    <artifactId>webmagic-extension</artifactId>
    <version>0.7.3</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>

</dependency>

2.开始第一个爬虫,我这是用了官方文档的事例,由于github我这好像访问不了,就给了一下访问地址 就是我的博客地址
代码如下 ,部分注释自带

package com.test;


import java.util.List;


import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;


public class GithubRepoPageProcessor implements PageProcessor {


    // 部分一:抓取网站的相关配置,包括编码、抓取间隔、重试次数等
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);


    
    // process是定制爬虫逻辑的核心接口,在这里编写抽取逻辑
    public void process(Page page) {
        // 部分二:定义如何抽取页面信息,并保存下来
        page.putField("author", page.getUrl().regex("https://blog.csdn\\.net/(\\w+)/.*").toString());
        page.putField("name", page.getHtml().xpath("//div[@class='push-event']/strong/a/text()").toString());
        System.out.println();
        List<String> contextList=page.getHtml().xpath("h4[@class='text-truncate']/a/text()").all();
        for(String str:contextList){
        System.out.println(str);
        }
        //System.out.println( page.getHtml().regex("h4[@class='text-truncate']/a/text()").all().toString());
        if (page.getResultItems().get("name") == null) {
            //skip this pagetimeline-on date-point
            page.setSkip(true);
        }
        page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));


        // 部分三:从页面发现后续的url地址来抓取
        //page.addTargetRequests(page.getHtml().links().regex("(https://blog.csdn\\.net/[\\w\\-]+/[\\w\\-]+)").all());
    }




    public Site getSite() {
        return site;
    }


    public static void main(String[] args) {


        Spider.create(new GithubRepoPageProcessor())
                //从"https://github.com/code4craft"开始抓
                .addUrl("https://blog.csdn.net/hanzl1")
                .addPipeline(new JsonFilePipeline("E:\\webmagic\\"))
                //开启5个线程抓取
                .thread(5)              
                //启动爬虫
                .run();
    }

}


3. 设置起始的url  https://blog.csdn.net/hanzl1

     .addUrl("https://blog.csdn.net/hanzl1")



分析了一下要抓的页面的信息   就是想抓一下页面的标题  



使用代码



运行结果对比  :



4.使用了这个框架后其实最主要的难点是分析内容 还有好多东西要

4.1.2 页面元素的抽取

第二部分是爬虫的核心部分:对于下载到的Html页面,你如何从中抽取到你想要的信息?WebMagic里主要使用了三种抽取技术:XPath、正则表达式和CSS选择器。另外,对于JSON格式的内容,可使用JsonPath进行解析。

  1. XPath

    XPath本来是用于XML中获取元素的一种查询语言,但是用于Html也是比较方便的。例如:

     page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()")
    

    这段代码使用了XPath,它的意思是“查找所有class属性为'entry-title public'的h1元素,并找到他的strong子节点的a子节点,并提取a节点的文本信息”。 对应的Html是这样子的:

    xpath-html

  2. CSS选择器

    CSS选择器是与XPath类似的语言。如果大家做过前端开发,肯定知道$('h1.entry-title')这种写法的含义。客观的说,它比XPath写起来要简单一些,但是如果写复杂一点的抽取规则,就相对要麻烦一点。        运行效果是一样的                                如:  List<String> contextList2=page.getHtml().css("h4[class='text-truncate'] a","text").all();

  3. 正则表达式

    正则表达式则是一种通用的文本抽取语言。

     page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());
    

    这段代码就用到了正则表达式,它表示匹配所有"https://github.com/code4craft/webmagic"这样的链接。我觉得这块挺难得,正则不太熟。

  4. JsonPath


猜你喜欢

转载自blog.csdn.net/hanzl1/article/details/80943686