Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse elements with only one class name included using Jsoup?

Tags:

java

jsoup

For this html:

<div id="list">
    <div class="one two three" date="20130121">
        ...
    </div>
    <div class="one" date="20130122">
        ...
    </div>
    <div class="one two" date="20130123">
        ...
    </div>
    <div class="one" date="20130124">
        ...
    </div>
</div>

I would like to extract the date element with class = "one" only, so that if class is included "one" but having other class is not correct.
My expect answer should be date="20130122" and date="20130124"

I tried to use:

Element outestDiv = doc.getElementById("list");
Elements eachDayBox = outestDiv.select("div.one");

But eachDayBox.size() return 4 but not 2. So how to extract with class only "one"?? Also, how to get element in "date"??

like image 460
jjLin Avatar asked Dec 20 '25 09:12

jjLin


2 Answers

Use [attribute=value] in select

Elements eachDayBox = outestDiv.select("div[class=one]"); //class only equal to one

Reference

like image 74
TheWhiteRabbit Avatar answered Dec 22 '25 23:12

TheWhiteRabbit


This will work.

Elements elements = doc.getElementsByAttributeValue("class", "one");
for(int i=0;i<elements.size();i++){
    Element tmp=elements.get(i);
    System.out.println(tmp.attr("date"));
}
like image 21
SANN3 Avatar answered Dec 22 '25 22:12

SANN3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!