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
下一篇: python 爬取新浪新闻正文内容