【发布时间】:2014-02-19 23:33:22
【问题描述】:
我正在尝试在谷歌新闻的热门故事部分中搜索所有标题。为了只获得热门故事部分的标题,我必须缩小到这个标签:
<div class="section top-stories-section" id=":2r">..</div>
这是我使用的代码(在 Eclipse 中):
public static void main(String[] args) throws IOException {
// fetches & parses HTML
String url = "http://news.google.com";
Document document = Jsoup.connect(url).get();
// Extract data
Element topStories = document.getElementById(":2r").;
Elements titles = topStories.select("span.titletext");
// Output data
for (Element title : titles) {
System.out.println("Title: " + title.text());
}
}
我似乎总是收到 NullPointerException。当我尝试像这样访问热门故事时,它也不起作用:
Element topStories = document.select("#:2r").first();
我错过了什么吗?这不应该工作吗?我对此比较陌生,请帮助并感谢您!
【问题讨论】:
标签: java html eclipse web-scraping jsoup