刚开始学习Java爬虫,跟着B站的视频做的一个小demo,可是结果就是运行不出来,希望知道哪里出现问题了,以及怎么改,谢谢大家了
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import us.codecraft.webmagic.*;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;
public class Demo {
public static void main(String[] args) {
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
Spider.create(new MyPageProcessor())
.addPipeline(new MyPipeline())
.setDownloader(httpClientDownloader)
.addUrl("http://www.sikiedu.com/")
.run();
}
}
class MyPageProcessor implements PageProcessor{
public void process(Page page) {
Document document = page.getHtml().getDocument();
Element span = document.select(".subtitle").get(0).previousElementSibling().select("span").get(0);
String text = span.text();
page.putField("title",text);
}
public Site getSite() {
return new Site();
}
}
class MyPipeline implements Pipeline{
public void process(ResultItems resultItems, Task task) {
String title = resultItems.get("title").toString();
System.out.println("拿到标题:"+title);
}
}

【相关推荐】
在官方文档的《4.3 使用Pipeline保存结果》中有“控制台输出结果”和“生成本地json文件的两种方案”,这一节的最后说第7章会讲“实现保存结果到文件、数据库等一系列功能。”结果没发现第7章 = = 。