Skip to content Skip to sidebar Skip to footer

Multiple Conditions In Beautifulsoup: Text=true & Img Alt=true

is there a way to use multiple conditions in BeautifulSoup? These are the two conditions I like to use together: Get text: soup.find_all(text=True) Get img alt: soup.find_all('img

Solution 1:

.find_all() returns only texts or tags, but you can make your own function that returns texts from the soup and text from the alt= attributes.

For example:

from bs4 import BeautifulSoup, Tag, NavigableString


txt = '''
Some text
<img alt="Some alt" src="#" />
Some other text
'''deftraverse(s):
    for c in s.contents:
        ifisinstance(c, Tag):
            if c.name == 'img'and'alt'in c.attrs:
                yield c['alt']
            yieldfrom traverse(c)
        elifisinstance(c, NavigableString):
            yield c


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

for text in traverse(soup):
    print(text.strip())

Prints:

Some text
Some alt
Some other text

Post a Comment for "Multiple Conditions In Beautifulsoup: Text=true & Img Alt=true"