刚开始学习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);
}
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/16329315439616.png "#left")