JenilDave
JenilDave

Reputation: 604

How to access multiple attributes of a tag using BeautifulSoup?

Say I have a HTML Tag <span id="sample" value="type-2" data-type="haulted" encryption_stat="False"></span>

I want to access values of its all attributes without using loops and list comprehension approach and I have a list of attributes to access ['data-type','encryption-stat','value'] of the span tag.

So basically I dont want to try this:

from bs4 import BeautifulSoup
s = '<span id="sample" value="type-2" data-type="haulted" encryption_stat="false"></span>'
data = BeautifulSoup(s,'html.parser')
values = []
for i in ['data-type','encryption_stat','value']:
    values.append(data.find('span',id='sample').get(i))

Neither the list comprehension hack.

Is there anyway to achieve the objective?

Upvotes: 0

Views: 207

Answers (3)

Andrej Kesely
Andrej Kesely

Reputation: 195438

If you don't want to use loops, you can use operator.itemgetter:

from bs4 import BeautifulSoup
from operator import itemgetter

s = '<span id="sample" value="type-2" data-type="haulted" encryption_stat="false"></span>'
data = BeautifulSoup(s,'html.parser')


attrs = ['data-type','encryption_stat','value']
span = data.select_one('#sample')

i = itemgetter(*attrs)
print(i(span.attrs))

Prints:

('haulted', 'false', 'type-2')

Or: To handle missing attributes, you can do:

i = itemgetter(*(span.attrs.keys() & attrs))
print(i(span.attrs))

Upvotes: 1

JenilDave
JenilDave

Reputation: 604

Yes so I found a way to my answer, I thought it would be worth contributing here for all.

I tried it out using map()

>>> list(map(data.find('span',id='sample').get,['data-type','encryption_stat','value']))

Another one was inspired from @KunduK's answer, using map()

>>> all = data.find('span',id='sample').attrs
>>> list(map(all.__getitem__,['data-type','encryption_stat','value']))

This also worked for me.

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Did you try with element.attrs and then find the value.

from bs4 import BeautifulSoup
s = '<span id="sample" value="type-2" data-type="haulted" encryption_stat="false"></span>'
data = BeautifulSoup(s,'html.parser')
item_attrs=data.find('span',id='sample').attrs
for item_attr in item_attrs:
    print(data.find('span',id='sample')[item_attr]) 

If you want to ignore any specific attribute use if clause.

from bs4 import BeautifulSoup
s = '<span id="sample" value="type-2" data-type="haulted" encryption_stat="false"></span>'
data = BeautifulSoup(s,'html.parser')
item_attrs=data.find('span',id='sample').attrs
for item_attr in item_attrs:
    if 'id' not in item_attr:
      print(data.find('span',id='sample')[item_attr])

Upvotes: 0

Related Questions