sarath
sarath

Reputation: 455

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.

Upvotes: 3

Views: 988

Answers (1)

StoopidDonut
StoopidDonut

Reputation: 8617

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]");

Upvotes: 3

Related Questions