Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting Elements that have multiple class whilst using JSoup

I am parsing some tables from a website, and specifically I am trying to extract the following cells by class name:

Elements e=d.select("span[class=bld lrg red]");

for (Element element : e) {
System.out.println(element.text());}

this code is giving me some values which is given as Price in the web site. but some cases, I want to take price from a different class rather than "span[class=bld lrg red]".

I mean is the "bld lrg red" class is empty then i want to take value from "span[class=price]"

How can I use 'or' in this case.. I mean if the "bld lrg red" class has value then take that price or take the "price" class value.

like image 296
sarath Avatar asked Nov 27 '25 06:11

sarath


1 Answers

You can also use regex to select the required elements in from your web page using jsoup too, there you can use and 'OR' condition to specify what you are looking for;

Example:

Elements e = d.select("span[class~=(?i)(bld lrg red|price)]");

The above regex would select your span elements with classselector directly matching bld lrg red OR price (case insensitive).

Refer to here for details: http://jsoup.org/apidocs/org/jsoup/select/Selector.html

Now you may want to iterate over the elements and select which are not blank, null, both and so on.

EDIT: As per the comments, price class is not held by a span element. To get over it, you can use:

Elements e = d.select("span[class=bld lrg red],del[class=price]");
like image 79
StoopidDonut Avatar answered Nov 29 '25 19:11

StoopidDonut



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!