package com.ij34; import us.codecraft.webmagic.Site; import us.codecraft.webmagic.Page; import us.codecraft.webmagic.Spider; import us.codecraft.webma... ...
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(); } }