如果您查看 Jsoup 从该页面获取的 HTML,您会看到类似
<div class="body">
<div class="pbk">
<span class="pg">adjective </span>
<div class="luna-Ent">
<span class="dnindex">1.</span>
<div class="dndata">
done, proceeding, or occurring with promptness or rapidity, as an action, process, etc.; prompt; immediate:
<span class="ital-inline">a quick response.</span>
</div>
</div>
<div class="luna-Ent">
<span class="dnindex">2.</span>
<div class="dndata">
that is over or completed within a short interval of time:
<span class="ital-inline">a quick shower.</span>
</div>
</div>
...
<div class="pbk">
<span class="pg">adverb </span>
<div class="luna-Ent">
<span class="dnindex">19.</span>
<div class="dndata">
<a style="font-style:normal; font-weight:normal;" href="/browse/quickly">quickly</a>.
</div>
</div>
</div>
所以每一节
adjective
1. done, proceeding, or occurring with promptness or rapidity...
2. that is over or completed within a short interval of time
...
14. Archaic.
a. endowed with life
b. having a high degree of vigor, energy, ...
noun
1. living persons; the quick and the dead
2. the tender, sensitive flesh of the living body...
...
adverb
...
在<div class="pbk">
其中包含<span class="pg">adjective </span>
部分名称和 divs 中的定义<div class="luna-Ent">
。所以你可以尝试做类似的事情
Document doc = Jsoup.connect("http://dictionary.reference.com/browse/quick").get();
Elements sections = doc.select("div.body div.pbk");
for (Element element : sections) {
String elementType = element.getElementsByClass("pg").text();
System.out.println("--------------------");
System.out.println(elementType);
for (Element definitions : element.getElementsByClass("luna-Ent"))
System.out.println(definitions.text());
}
此代码将选择所有部分,并element.getElementsByClass("pg")
使用它们在具有类的 div 中的事实来查找部分的名称和定义luna-Ent
element.getElementsByClass("luna-Ent")
(如果您想跳过数字1.
,2.
您可以选择dndata
类代替luna-Ent
)
输出:
--------------------
adjective
1. done, proceeding, or occurring with promptness or rapidity, as an action, process, etc.; prompt; immediate: a quick response.
2. that is over or completed within a short interval of time: a quick shower.
3. moving, or able to move, with speed: a quick fox; a quick train.
4. swift or rapid, as motion: a quick flick of the wrist.
5. easily provoked or excited; hasty: a quick temper.
6. keenly responsive; lively; acute: a quick wit.
7. acting with swiftness or rapidity: a quick worker.
8. prompt or swift to do something: quick to respond.
9. prompt to perceive; sensitive: a quick eye.
10. prompt to understand, learn, etc.; of ready intelligence: a quick student.
11. (of a bend or curve) sharp: a quick bend in the road.
12. consisting of living plants: a quick pot of flowers.
13. brisk, as fire, flames, heat, etc.
14. Archaic. a. endowed with life. b. having a high degree of vigor, energy, or activity.
--------------------
noun
15. living persons: the quick and the dead.
16. the tender, sensitive flesh of the living body, especially that under the nails: nails bitten down to the quick.
17. the vital or most important part.
18. Chiefly British. a. a line of shrubs or plants, especially of hawthorn, forming a hedge. b. a single shrub or plant in such a hedge.
--------------------
adverb
19. quickly.