Jul-08-2019, 05:06 PM
How it works.
To find mark tag.
from bs4 import BeautifulSoup html = '''\ <span><mark class="search-item__highlight">Google</mark> has warned the Trump administration</span>''' soup = BeautifulSoup(html, 'lxml')Use:
>>> span_tag = soup.find('span') >>> span_tag <span><mark class="search-item__highlight">Google</mark> has warned the Trump administration</span> >>> span_tag.text 'Google has warned the Trump administration'Span tag
.text
has has output as show over,only that Google
is highlighted when html is rendered.To find mark tag.
>>> mark_tag = span_tag.find('mark') >>> mark_tag <mark class="search-item__highlight">Google</mark> >>> mark_tag.text 'Google' # The CSS class name can be found with attrs >>> mark_tag.attrs {'class': ['search-item__highlight']}