Aditya
Aditya

Reputation: 35

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>

Upvotes: 3

Views: 859

Answers (2)

jmaloney13
jmaloney13

Reputation: 235

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.

Upvotes: 1

jmaloney13
jmaloney13

Reputation: 235

@Aditya, I think soup.find("span") will only return the first "span" and you want the text from the second one. I would try:

for item in soup.select("div._9uwBC.wY0my"):
    spans = item.find_all("span")
    for span in spans:
        n = span.text
        if n != '':
            print(n)

Which should print the text of the non-empty span tags, under the you specified. Does accomplish what you want?

Upvotes: 2

Related Questions