【发布时间】:2014-02-19 00:56:18
【问题描述】:
我正在尝试在 Eclipse 中运行一个网络爬虫,它使用 Jsoup 可以获取此页面上教授的姓名:yu.edu/faculty 并将它们打印出来。这是我的代码:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class YUscraper {
public static void main(String[] args) throws IOException {
// fetches & parses HTML
String url = "http://yu.edu/faculty/";
Document document = Jsoup.connect(url).get();
// Extract data
Element content = document.getElementById("mainlist");
Elements names = content.getElementsByTag("a");
// Output data
for (Element name : names) {
System.out.println("Name: " + name.text());
}
}
}
我收到此错误:
Exception in thread "main" java.lang.NullPointerException
at YUscraper.main(YUscraper.java:18)
我对此比较陌生,所以如果我遗漏了一些非常明显的东西,请原谅我。我使用了很多我见过的例子来达到这一点,但我仍然不明白 throws IOException 的用途以及发现异常意味着什么。请帮忙,谢谢!
【问题讨论】:
标签: java eclipse web-scraping jsoup