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.
Asked
Active
Viewed 325 times
1

Slavka
- 1,070
- 4
- 13
- 28
-
Possible duplicate [Extracting an attribute value with beautifulsoup](https://stackoverflow.com/questions/2612548/extracting-an-attribute-value-with-beautifulsoup) – Alif Jahan Mar 08 '19 at 10:06
-
no, there is question about fixed attribute value, I can't fix this value, I need to find it:) – Slavka Mar 08 '19 at 10:08
2 Answers
1
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)

Maaz
- 2,405
- 1
- 15
- 21
1
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)

QHarr
- 83,427
- 12
- 54
- 101