I want to call Beautiful Soup attributes (e.g. class_, href, id) from a variable so that I can use it in functions like this:
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[???]
Expected Result:
<p>xxx </p> <p id="2">yyy</p> <p> zzz</p>
MYGz solved the first problem using tag, argdict , using a dictionary as an argument to the function. Then I found **kwargs in this question (pass the key and value of the dictionary).
But I did not find a way for del x["class"] . How to pass a class key? I tried using ckey=kwargs.keys() and then del x[ckey] , but that didn't work.
ps1: any idea why removeAttrib (data, "p", {"class": "story"}) doesn't work? Ps2: This is another topic than this (this is not a duplicate)
JinSnow Jan 22 '17 at 15:29 2017-01-22 15:29
source share