Here are alternative ways to add an attribute ...
Quote:
Attributes¶
A tag may have any number of attributes. The tag <b id="boldest"> has an attribute “id” whose value is “boldest”. You can access a tag’s attributes by treating the tag like a dictionary:
tag = BeautifulSoup('<b id="boldest">bold</b>', 'html.parser').b
tag['id']
# 'boldest'
You can access that dictionary directly as .attrs:
tag.attrs
# {'id': 'boldest'}
You can add, remove, and modify a tag’s attributes. Again, this is done by treating the tag as a dictionary:
tag['id'] = 'verybold'
tag['another-attribute'] = 1
tag
# <b another-attribute="1" id="verybold"></b>
del tag['id']
del tag['another-attribute']
tag
# <b>bold</b>
tag['id']
# KeyError: 'id'
tag.get('id')
# None
|
So I would remove the attrs= parameter on the new tag method, and instead create the tag then either use the new tag in its dict mode to add the attributes needed one by one or assign it to the tags's .attrs if possible.