0

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1592380
  • 34,265
  • 92
  • 284
  • 515

1 Answers1

3

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]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343