Solution 1 :

I’m using BeautifulSoup to parse the HTML you provided.

Then I navigate the soup using the spans. After I have the target element, I get the text of the element.

soup.span.span.text

or

This finds all spans and selects the second one.

soup.find_all('span')[1]

I have this additional code before calling either of those.

from bs4 import BeautifulSoup

html = "<span class="posting-location go-to-posting">Santa Gertrudes , <span> Tatuapé, São Paulo</span></span>"

soup = BeautifulSoup(html, 'html.parser')

Problem :

I have the following html piece

    soup = <span class="posting-location go-to-posting">
                                Santa Gertrudes , 
                                <span> Tatuapé, São Paulo</span>
    </span>

I know that, to access the “Tatuapé, São Paulo”, I can use

soup.select_one('span')

However, how do I select “Santa Gertrudes , “?

Comments

Comment posted by Jacob Evans

I’ve been trying to figure this out for weeks, you just saved my job! I’ll probably hold off until Friday to implement.

Comment posted by Johnathan Irvin

@JacobEvans I’m glad you found this useful!

By

Leave a Reply

Your email address will not be published. Required fields are marked *