pbount
pbount

Reputation: 1803

Python BeautifulSoup get attribute values from any element containing an attribute

In beautifulSoup I can target all elements by tag, i.e:

BeautifulSoup().find_all('img')

or by src attribute as long as I also specify an attribute value, i.e:

BeautifulSoup().find_all(attrs={"src": "some.domain.com/file."})

How can I target all elements with an src attribute regardless of whatever that src attributes value is?

Upvotes: 0

Views: 423

Answers (2)

KunduK
KunduK

Reputation: 33384

If you have bs4 4.7.1 or above you can use the following css selector.

for item in soup.select('[src]'):
    print(item)

Upvotes: 1

Max Power
Max Power

Reputation: 8954

Does this work for you?

BeautifulSoup().find_all(attrs={"src": re.compile('*')})

Upvotes: 1

Related Questions