Home > Software engineering >  How do I scrape data from a tag belonging to the same label and the same class? BeautifulSoup
How do I scrape data from a tag belonging to the same label and the same class? BeautifulSoup

Time:12-25

I have a tag with the same tag and the same name(property). Here is my code

first_movie.find('p',{'class' : 'sort-num_votes-visible'})

Here is my output

<p >
<span >Votes:</span>
<span data-value="978272" name="nv">978,272</span>
<span >|</span> <span >Gross:</span>
<span data-value="858,373,000" name="nv">$858.37M</span>
</p>

I'm reaching span tag this code;

first_movie.find('span', {'name':'nv',"data-value": True})

978272 --> output

But i want reach the other value with named nv ($858.37M). My code is only getting this value (978,272) because tags names is equal each other (name = nv)

CodePudding user response:

You're close.

Try using find_all and then grab the last element.

For example:

from bs4 import BeautifulSoup

html_sample = '''
<p >
<span >Votes:</span>
<span data-value="978272" name="nv">978,272</span>
<span >|</span> <span >Gross:</span>
<span data-value="858,373,000" name="nv">$858.37M</span>
</p>
'''

soup = (
    BeautifulSoup(html_sample, "lxml")
    .find_all("span", {'name':'nv',"data-value": True})
)
print(soup[-1].getText())

Output:

$858.37M

CodePudding user response:

If you reach for all spans in p tag, you can work with them like with list and use index to reach for last div.

movies = soup.find('p',{'class' : 'sort-num_votes-visible'})

my_movie = movies.findAll('span')

my_span = my_movie[3].text
  • Related