欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

webmagic 爬取网页所有文章的标题时间作者和内容

程序员文章站 2022-03-02 19:38:55
...
package com.ij34;

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

import java.util.List;

public class HuxiuTest implements PageProcessor {
    @Override
    public void process(Page page) {
        List<String> requests = page.getHtml().links().regex(".*article.*").all();
         page.addTargetRequests(requests);
         page.putField("标题",page.getHtml().xpath("//div[@class='wrap-left pull-left']//h1/text()"));
         page.putField("作者", page.getHtml().xpath("//div[@class='article-author']//a/text()"));
         page.putField("时间", page.getHtml().xpath("//div[@class='column-link-box']/span[1]//text()"));
         page.putField("内容",page.getHtml().xpath("//div[@class='article-content-wrap']"));

    }
    @Override
    public Site getSite() {
        return Site.me().setDomain("www.huxiu.com");
    }

    public static void main(String[] args) {
        Spider.create(new HuxiuTest()).addUrl("https://www.huxiu.com/")
       .addPipeline(new FilePipeline("D:\\webmagic\\data\\"))
        .run();
    }

}

 


更多专业前端知识,请上【猿2048】www.mk2048.com