1

I am looking to retrieve the value "312 votes" from the below tag hierarchy:

<div class="rating-rank right">
<span class="rating-votes-div-65211">312 votes</span>
</div>

The problem seems to be that the span tag has a unique identifier for every values in the page. In the above case '65211'. What should i do to retrieve the required value?

I am using soup.select to get the values. But it doesn't seem to work.

for tag in soup.select('div.rating-rank right'):
    try:
        print(tag.string)
    except KeyError:
        pass

1 Answer 1

2

You try to select a right element that follows a div with class rating-rank. You can select what you want like this:

soup.select("div.rating-rank.right span")

With css selectors you have to read them from right to left. So div.rating-rank.right span means I want a span element which is after a div element having rating-rank, right as classes. From the moment you identified your span elements, you can print their contents like you already do.

Sign up to request clarification or add additional context in comments.

4 Comments

yes it worked wonderfully. :) Could you pls briefly explain how this worked?
Maybe it's better to tell me what you don't understand
like what does the dot and the space signify?
For your particular case I explain it in the answer. But it's a very broad subject to explain in general. You can check this resource to find more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.