Dim
Dim

Reputation: 4807

Android Jsoup find specific element

I wish to parse IMBD page to get the movie rating. (Please do not offer me APIs). This is my code (for now):

private static class getData extends AsyncTask<String, String, Void>
{

    String url = "https://www.imdb.com/title/tt0437086/";


    @Override
    protected Void doInBackground(String... strings) {

        try {
            Document document = Jsoup.connect(url).get();
            Elements img = document.select("span");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

I get all the span, but do I need to cycle all of them to find the rating? What I need is the rating from this line (specifically the rating itself):

<span itemprop="ratingValue">7.5</span>

How can I get the rating without cycling trough all elements?

Upvotes: 1

Views: 30

Answers (1)

guipivoto
guipivoto

Reputation: 18677

You can select a specific span

Elements img = document.select("span[itemprop=ratingValue]");
Log.e("TEST", "Result: " + img.text());

I tested here and it is properly printing 7.5

You can find more info about the selector syntax here

Upvotes: 2

Related Questions