【发布时间】:2013-12-05 15:15:08
【问题描述】:
我正在解析一个网页,我可以解析 id 但不能解析类。这就是我的意思;我正在解析整个新闻包装器 ID:
protected String getBlogStats() throws Exception {
String result = "";
// get html document structure
Document document = Jsoup.connect(BLOG_URL).get();
// selector query
Elements nodeBlogStats = document.select("div#news_wrapper");
// check results
if(nodeBlogStats.size() > 0) {
// get value
result = nodeBlogStats.get(0).text();
}
// return
return result;
}
它有效,我可以显示所有内容,但我只需要选择这个包装器中的 h3 标签,所以我尝试了这种方式:
protected String getBlogStats() throws Exception {
String result = "";
// get html document structure
Document document = Jsoup.connect(BLOG_URL).get();
// selector query
Elements nodeBlogStats = document.select("div#news_wrapper .news-col-1 h3");
// check results
if(nodeBlogStats.size() > 0) {
// get value
result = nodeBlogStats.get(0).text();
}
// return
return result;
}
其中news-col-1 是一个类.. 但活动是空白的.. 是否有另一种方法可以编写类以使用 jsoup 进行解析?谢谢
【问题讨论】:
-
你可以这样写 getElementsByClassName("yourclassname")
-
您能分享一下您要解析的网址吗?
-
@NitinMisra 你能告诉我如何处理你的建议吗?我分享了网址
-
如果我写:
Elements nodeBlogStats = document.select("div#news_wrapper h3");它只显示第一个而不是所有h3标签
标签: java android class html-parsing jsoup