简体   繁体   中英

beautifulsoup - Fetching text either side of a br tag

I have unfortunately become stuck with the following problem:

 <a href="someurl"> 
"TEXT ONE"
 <br>
 "TEXT TWO"
 </a>

I need text one and text two separately. I can only obtain them as a whole providing "TEXT ONE TEXT TWO" by using "text = container.a.text" , with the container being a parent of the a tags. I have tried as many ways as I could find with no success. I can't manage to use br tag properly.
Thank you for any help.

I would avoid relying on the presence of the br element and would instead locate all the text nodes inside the a :

In [1]: from bs4 import BeautifulSoup

In [2]: html = """ <a href="someurl"> 
    ...: "TEXT ONE"
    ...:  <br>
    ...:  "TEXT TWO"
    ...:  </a>"""

In [3]: soup = BeautifulSoup(html, "html.parser")

In [4]: [item.strip() for item in soup.a(text=True)]
Out[4]: ['"TEXT ONE"', '"TEXT TWO"']

Note that a(text=True) is a short version of a.find_all(text=True) .


You can, of course, unpack it into separate variables if needed :

In [5]: text_one, text_two = [item.strip() for item in soup.a(text=True)]

In [6]: text_one
Out[6]: '"TEXT ONE"'

In [7]: text_two
Out[7]: '"TEXT TWO"'

You could use .previousSibiling and .nextSibling attributes after finding the br tag:

>>> container.a.find("br").previousSibling
' \n"TEXT ONE"\n '
>>> container.a.find("br").nextSibling
'\n "TEXT TWO"\n '

You can do the same in several ways. Here is another way:

from bs4 import BeautifulSoup

content='''
 <a href="someurl"> 
"TEXT ONE"
 <br>
 "TEXT TWO"
 </a>
'''
soup = BeautifulSoup(content,'lxml')
for items in soup.select('a'):
    elem = [' '.join(item.split()) for item in items.strings]
    print(elem)

Output:

['"TEXT ONE"', '"TEXT TWO"']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM