Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BS4 text inside <span> which has no class

i am trying to scrape this 4.1 rating in span tag using this python code but it is returning empty.

for item in soup.select("._9uwBC wY0my"):
        n = soup.find("span").text()
        print(n)
---------------------------------------

<div class="_9uwBC wY0my">
      <span class="icon-star _537e4"></span>
      <span>4.1</span>
</div>
like image 655
Aditya Avatar asked Apr 15 '26 22:04

Aditya


1 Answers

OK, here's one approach for getting the names and stars for each restaurant on the page. It's not necessarily the most elegant way to do it, but I've tried it a couple of times and it seems to work:

divs = soup.find_all('div')

for div in divs:
    if div.has_attr('class'):
        if div['class'] == ['nA6kb']: ## the class of the divs with the name
            name = div.text
            k = div.find_next('div') ## the next div
            l = k.find_next('div')  ## the div with the stars
            spans = l.find_all('span') ## this part is same as the answer above
            for span in spans:
                n = span.text
                if n != '':
                    print(name, n)

This assumes that the div that contains the stars span is always the second div after the div that contains the restaurant name. It looks like that's always the case, but I'm not positive that it never changes.

like image 99
jmaloney13 Avatar answered Apr 18 '26 12:04

jmaloney13