Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

java - Jsoup select div having multiple classes

I am trying to select, using Jsoup, a <div> that has multiple classes:

<div class="content-text right-align bold-font">...</div>

The syntax for doing so, to the best of my understanding, should be:

document.select("div.content-text.right-align.bold-font");

However, for some reason, this doesn't work for me.

When I try the same exact syntax on JSFIDDLE, it works without a hitch.

Does multi-class selection work in Jsoup?

(I'd rather find out that this is a bug in my code than find out that this is a Jsoup limitation :)

UPDATE (thanks to the answer below): Jsoup works perfectly with the aforementioned syntax.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Works for me with latest Jsoup (1.5.2).

String html = "<div class="content-text right-align bold-font">foo</div>";
Document document = Jsoup.parse(html);
Elements elements = document.select("div.content-text.right-align.bold-font");
System.out.println(elements.text()); // foo

So either you're possibly using an outdated version of Jsoup which exposes a bug related to this, or the actual HTML doesn't contain a <div> like that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...