我正在寻找如何使用Jsoup
提取CNN或NewYork等新闻文章的内容。
事实上我曾尝试过以下代码:
Document document = Jsoup.connect("http://edition.cnn.com/2013/11/10/world/asia/philippines-typhoon-haiyan/index.html").get();
Element contents = document.select("#content").first();
System.out.println(contents.html());
System.out.println(contents.text());
我收到了这个错误:
Exception in thread "main" java.lang.NullPointerException
at com.clearforest.Test.main(Test.java:36)
请您知道如何从文章中提取正确的文本。
答案 0 :(得分:1)
contents Element
调用后select
为空 - 您指定的选择器在从CNN下载的文档中没有返回匹配项 - 请尝试返回故事div内容的document.select("div.cnn_strycntntlft")
。