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
'''
def traverse(s):
for c in s.contents:
if isinstance(c, Tag):
if c.name == 'img' and 'alt' in c.attrs:
yield c['alt']
yield from traverse(c)
elif isinstance(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"