EddiRae
EddiRae

Reputation: 47

Jsoup get class in span with partial class name

I have a website that has a different "pid" in a class for each page that I need to go to.

Below is the website HTML. The "348" is what changes:

 <span class="arial_20 redFont   pid-348-pc" dir="ltr">-5.60</span>

Below is the code:

Document doc1;
try
{
    doc1 = Jsoup.connect(sChartLink).get();         
    String Title = doc1.title();
    System.out.println("Title = " + Title + "\n");

    String sCurrentPrice = doc1.select("span#last_last").text(); //Get Current Price
    System.out.println("Current Price = " + sCurrentPrice + "\n");

    String sPriceChange = doc1.select("span[class=arial_20 redFont   pid-348-pc]").text(); //Get Price Change
    System.out.println("Price Change = " + sPriceChange + "\n");
}
catch (IOException e) {
    e.printStackTrace();
}

I was wondering how to search leaving out the pid number.

Upvotes: 1

Views: 789

Answers (1)

glytching
glytching

Reputation: 47905

You can use a CSS selector specifying a partial class name.

For example:

String html = "<html>" +
        "<span class=\"arial_20 redFont   pid-348-pc\" dir=\"ltr\">-5.60</span>" +
        "<span class=\"arial_20 redFont \" dir=\"ltr\">55.80</span>" +
        "</html>";

Document doc = Jsoup.parse(html);

// this will print out -5.60 since the only span with a class matching 'arial_20 redFont   pid-*'
// is the one with the value: -5.60
// the other span does not match that CSS selector
String sPriceChange = doc.select("span[class*=\"arial_20 redFont   pid-\"]").text();
System.out.println("Price Change = " + sPriceChange + "\n");

Upvotes: 1

Related Questions