Search code examples
javaweb-scrapingjsoup

JSoup get element Span


I am working with JSoup and this is my code:

public class ClassOLX {

public static final String URL = "https://www.olx.com.pe/item/nuevo-nissan-march-autoland-iid-1103776672";

public static void main (String args[]) throws IOException  {
    
    if (getStatusConnectionCode(URL) == 200) {
        Document document = getHtmlDocument(URL);
        String model = document.select(".rui-2CYS9").select(".itemPrice").text();
        System.out.println("Model: "+model);
    }else
        System.out.println(getStatusConnectionCode(URL));
}


public static int getStatusConnectionCode(String url) {
    
    Response response = null;
    try {
        response = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(100000).ignoreHttpErrors(true).execute();
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
    return response.statusCode();
}

public static Document getHtmlDocument(String url) {

    Document doc = null;
        try {
            doc = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(100000).get();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
    return doc;
}
}

This is the page:

enter image description here

I want to get the values of the following elements : itemPrice,_18gRm,itemTitle,_2FRXm

Thanks for all.


Solution

  • All you have to do is to use the following class selectors and get the text attribute-

    String price = doc.select("._2xKfz").text();
    String year = doc.select("._18gRm").text();
    String title = doc.select("._3rJ6e").text();
    String place = doc.select("._2FRXm").text();
    

    And it will get you the desired data.