Jsoup select 不同的 Div 类型并按顺序处理它们
Jsoup select different Div types and handle them sequentially
您好,我正在使用 Jsoup
抓取一个 html 页面,我有 2 个 div
彼此相邻:
<div class="time_head">... </div>
<div class="blockfix">... </div>
我需要获取第一个 time_head
,然后将第一个 blockfix
中的所有元素放入其中。依此类推。
到目前为止我尝试了什么:
Elements time_heads = doc.select("time_head");
for (Element time_head : time_heads) {
String the_time_head = Jsoup.parse(String.valueOf(time_head.getElementsByClass("time_head"))).text();
Log.i("the_time_head ", " Hz: "+the_time_head);
}
Elements blockfixs = doc.select("blockfix");
for (Element blockfix : blockfixs) {
String the_blockfix = Jsoup.parse(String.valueOf(time_head.getElementsByClass("blockfix"))).text();
Log.i("the_blockfix ", " Hz: "+the_blockfix);
}
我需要这样的结果:
time_head1:
---- blockfix elemts1
---- blockfix elemts2
---- blockfix elemts3
time_head2:
---- blockfix elemts1
---- blockfix elemts2
---- blockfix elemts3
您似乎想要遍历所有具有 time_head
或 blockfix
的 div
元素,并根据您的发现以不同方式打印它。在这种情况下,您可以将 select(CSSquery)
与 elementA, elementB
之类的查询一起使用,因为 ,
可以在 CSS 中被视为 OR 运算符。然后根据 class 当前迭代元素的名称选择如何处理它。
演示:
String html =
"<div class='time_head'>time_head content1</div>"
+ "<div class='blockfix'>blockfix1</div>"
+ "<div class='blockfix'>blockfix2</div>"
+ "<div class='time_head'>time_head content2</div>"
+ "<div class='blockfix'>blockfix3</div>"
+ "<div class='blockfix'>blockfix4</div>";
Document doc = Jsoup.parse(html);
for (Element el : doc.select("div.time_head, div.blockfix")) {
if (el.className().equalsIgnoreCase("time_head")) {
//handle time_head here
System.out.println(el.text().toUpperCase());
}
if (el.className().equalsIgnoreCase("blockfix")) {
//handle blockfix here
System.out.println("----"+el.text());
}
}
输出:
TIME_HEAD CONTENT1
----blockfix1
----blockfix2
TIME_HEAD CONTENT2
----blockfix3
----blockfix4
您好,我正在使用 Jsoup
抓取一个 html 页面,我有 2 个 div
彼此相邻:
<div class="time_head">... </div>
<div class="blockfix">... </div>
我需要获取第一个 time_head
,然后将第一个 blockfix
中的所有元素放入其中。依此类推。
到目前为止我尝试了什么:
Elements time_heads = doc.select("time_head");
for (Element time_head : time_heads) {
String the_time_head = Jsoup.parse(String.valueOf(time_head.getElementsByClass("time_head"))).text();
Log.i("the_time_head ", " Hz: "+the_time_head);
}
Elements blockfixs = doc.select("blockfix");
for (Element blockfix : blockfixs) {
String the_blockfix = Jsoup.parse(String.valueOf(time_head.getElementsByClass("blockfix"))).text();
Log.i("the_blockfix ", " Hz: "+the_blockfix);
}
我需要这样的结果:
time_head1:
---- blockfix elemts1
---- blockfix elemts2
---- blockfix elemts3
time_head2:
---- blockfix elemts1
---- blockfix elemts2
---- blockfix elemts3
您似乎想要遍历所有具有 time_head
或 blockfix
的 div
元素,并根据您的发现以不同方式打印它。在这种情况下,您可以将 select(CSSquery)
与 elementA, elementB
之类的查询一起使用,因为 ,
可以在 CSS 中被视为 OR 运算符。然后根据 class 当前迭代元素的名称选择如何处理它。
演示:
String html =
"<div class='time_head'>time_head content1</div>"
+ "<div class='blockfix'>blockfix1</div>"
+ "<div class='blockfix'>blockfix2</div>"
+ "<div class='time_head'>time_head content2</div>"
+ "<div class='blockfix'>blockfix3</div>"
+ "<div class='blockfix'>blockfix4</div>";
Document doc = Jsoup.parse(html);
for (Element el : doc.select("div.time_head, div.blockfix")) {
if (el.className().equalsIgnoreCase("time_head")) {
//handle time_head here
System.out.println(el.text().toUpperCase());
}
if (el.className().equalsIgnoreCase("blockfix")) {
//handle blockfix here
System.out.println("----"+el.text());
}
}
输出:
TIME_HEAD CONTENT1
----blockfix1
----blockfix2
TIME_HEAD CONTENT2
----blockfix3
----blockfix4