Slavka
Slavka

Reputation: 1148

BeautifulSoup find attribute value in any tag

How to find value of certain attribute using bs4? For example, I need to find all values of src attribute, it could be in any tag of my html document.

Upvotes: 1

Views: 345

Answers (2)

QHarr
QHarr

Reputation: 84465

Just use an attribute selector (that's what it's intended for). More efficient.

values = [item['src'] for item in soup.select('[src]')]

You can extend by adding the required string/substring of a desired value by adding = substring/string after the attribute i.e. [src="mystring"]

Example:

import requests
from bs4 import BeautifulSoup as bs

res = requests.get('https://stackoverflow.com/questions/55060825/beautifulsoup-find-attribute-value-in-any-tag/55062258#55062258')
soup = bs(res.content, 'lxml')
values = [item['src'] for item in soup.select('[src]')]
print(values)

Upvotes: 1

Maaz
Maaz

Reputation: 2445

You can do something like this:

from bs4 import BeautifulSoup
import requests

r = requests.get('http://your.url')
soup = BeautifulSoup(r.text,'html.parser')

attr_src = []
for tag in soup():
    if 'src' in tag.attrs:
        attr_src.append(tag.get('src'))
print(attr_src)

Upvotes: 1

Related Questions