I want to call a Beautiful Soup attributes (eg. class_, href, id) from a variable to use it in functions such as this one:
script
from bs4 import BeautifulSoup
data='<p class="story">xxx </p> <p id="2">yyy</p> <p class="story"> zzz</p>'
def removeAttrib(data, **kwarg):
soup = BeautifulSoup(data, "html.parser")
for x in soup.findAll(tag, kwargs):
del x[???] # should be an equivalent of: del x["class"]
kwargs= {"class":"story"}
removeAttrib(data,"p",**kwargs )
print(soup)
expected result:
<p>xxx </p> <p id="2">yyy</p> <p> zzz</p>
MYGz solved the first issue using tag, argdict
using a a dictionary as argument for the function. I then found in this question the **kwargs
(to pass the dictionary key and value).
But I did not find the way for the del x["class"]
. How to pass the "class" key? I tried using ckey=kwargs.keys()
and then del x[ckey]
but it did not work.
ps1: any idea why removeAttrib(data, "p", {"class": "story"}) doesn't work? Ps2: This is another topic than this (it's not a duplicate)