我正在解析一个网页,我可以解析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解析时编写类?感谢
答案 0 :(得分:2)
使用类获取该div中所有h3标签的一种可能方法是:
Elements nodeBlogStats = doc.select("div.news-col-0 h3");
它不适用于.news-col-1
,因为<h3>
标签都不是该div的直接子节点。正如您所评论的那样,ID也可以起作用:
Elements nodeBlogStats = doc.select("div#news_wrapper h3");
你的代码只返回第一个h3而不是全部的原因是因为你将结果设置为nodeBlogStats
中的第一个元素的文本(当你说get(0)
时):
if(nodeBlogStats.size() > 0) {
// get value
result = nodeBlogStats.get(0).text();
}
如果您想要所有h3文字,请考虑返回List
或nodeBlogStats.text()
。
更新:
因此,您可以更改方法以返回ArrayList
。
protected ArrayList<String> getBlogStats() throws Exception {
// get html document structure
Document document = Jsoup.connect(BLOG_URL).get();
// selector query
Elements nodeBlogStats = document.select("div#news_wrapper");
// check results
ArrayList<String> list = new ArrayList<String>();
for (Element e : nodeBlogStats) {
list.add(e.text());
}
return list;
}