Question

I'm experimenting with http://robobrowser.readthedocs.org/en/latest/readme.html, a new python library based on the beautiful soup library.

I have the following HTML:

I have the following django view function

def index(request):    

    p=str(request.POST.get('p', False)) # eg. p='https://www.yahoo.com/'
    browser = RoboBrowser(history=True)
    postedmessage = browser.open(p)
    out = browser.select('span.select')

    return HttpResponse(postedmessage)

out yields:

<span class="select"><a href="/selector/1">select</a></span>

But how do I select the inner tag using beautiful soup?

Était-ce utile?

La solution

Simply add a to your CSS selector passed to the soup.select() method:

out = browser.select('span.select a')

Alternatively, navigate to the first matching child tag by addressing it by tagname as an attribute:

out = browser.select('span.select')
links = [span.a for span in out]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top