问题描述
问题是我设法从互联网上带来了我想要的数据,但是它带有一个我想删除的标签。我从这里得到数据:
<a style="display: block;" class="btn-collapse" onclick="collapseChapter('collapsible490362')" role="button">
<i class="fa fa-chevron-down fa-fw"></i>
Capítulo 120.00
</a>
数据使我感兴趣的是:Capítulo120.00 。问题是在我的应用程序中,它看起来像这样:
这是我带来数据的方式:
protected ArrayList<TMODatosSeleccion> doInBackground(Void... voids) {
String url = getIntent().getStringExtra("valor");
tmoDatosSeleccions.clear();
try {
Document doc = Jsoup.connect(url).get();
Elements data = doc.select("div.col-10.text-truncate");
Elements dataDos = doc.select("div.col-2.col-sm-1.text-right");
for (Element e1 : data) {
for(Element e2 : dataDos){
String numeroCap = e1.select("a").html();
String urlManga = e2.select("a").attr("href");
tmoDatosSeleccions.add(new TMODatosSeleccion(numeroCap,urlManga));
}
}
} catch (IOException e) {
e.printstacktrace();
}
return tmoDatosSeleccions;
}
但这在我的TextView
中看起来像这样:
<i class="fa fa-chevron-down fa-fw"></i> Capítulo 120.00
就像我说的,我只想看到Cápitulo120.00
有人知道我该如何解决吗?
解决方法
要选择文本,请使用String numeroCap = e1.select(“ a”)。text()代替html()
。 html()
方法选择元素内的所有内容。
public static void main(String[] args) {
String html = "<a style=\"display: block;\" class=\"btn-collapse\" onclick=\"collapseChapter('collapsible490362')\" role=\"button\"> <i class=\"fa fa-chevron-down fa-fw\"></i> Capítulo 120.00 </a>";
Document doc = Jsoup.parse(html);
String text = doc.select("a").text();
System.out.print(text);
}