1

Today I was looking into a small file uploader and I got the following response from the API page.

upload_success<br>http://www.filepup.net/files/R6wVq1405781467.html<br>http://www.filepup.net/delete/Jp3q5w1405781467/R6wVq1405781467.html

I need to get the part between the two <br> tags. I am using Beautifulsoup and this code but it returns None.

fpbs = BeautifulSoup(filepup.text)
finallink = fpbs.find('br', 'br')
print(finallink)

1 Answer 1

2

You cannot search for text between two tags, no. You can locate the first <br> tag, then take its next sibling, however:

>>> soup = BeautifulSoup('upload_success<br>http://www.filepup.net/files/R6wVq1405781467.html<br>http://www.filepup.net/delete/Jp3q5w1405781467/R6wVq1405781467.html')
>>> soup.find('br')
<br/>
>>> soup.find('br').next_sibling
u'http://www.filepup.net/files/R6wVq1405781467.html'

You could use a CSS selector search to search for an adjacent sibling, then grab the preceding sibling; to CSS only the tags are siblings, but to BeautifulSoup the text nodes count too.

The adjacent select is + between two CSS selectors, and selects the second of the two; br + br would select any br tag that comes second.

Together with a parent node (say a specific id or class) that can be a very powerful combination:

>>> soup = BeautifulSoup('''\
... <div id="div1">
...     some text
...     <br/>
...     some target text
...     <br/>
...     foo bar
... </div>
... <div id="div2">
...     some more text
...     <br/>
...     select me, ooh, pick me!
...     <br/>
...     fooed the bar!
... </div>
... ''')
>>> soup.select('#div2 br + br')[0]
<br/>
>>> soup.select('#div2 br + br')[0].previous_sibling
u'\n    select me, ooh, pick me!\n    '

This picked a very specific text node between two <br> tags, in a specific <div> tag.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.