K
K
KirillTrueno2021-12-04 22:06:32
Python
KirillTrueno, 2021-12-04 22:06:32

BeautifulSoup: how to find element by attribute only without specifying tag name?

For example, a page might have one of the article title options:

<h1 itemprop="headline">Текст</h1>
<h3 itemprop="headline">Текст</h3>
<p itemprop="headline">Текст</p>

etc.

How can I use BeautifulSoup to find the line where itemprop="headline" without listing all possible tags?

I'm trying this, but it gives me an error:
soup.select_one('*',{'itemprop' : 'headline'}).text

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-12-04
@KirillTrueno

Just pass the tag attributes, no name. This is an optional parameter
soup.find(attrs={'itemprop': 'headline'})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question