Calling and using an attribute stored in variable (using Beautifulsoup 4)

前端 未结 2 380
走了就别回头了
走了就别回头了 2021-01-23 11:09

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 b         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-23 11:37

    all credit to MYGz and commandlineluser

    from bs4 import BeautifulSoup
    data='

    xxx

    yyy

    zzz

    ' def removeAttrib(data, tag, kwargs): soup = BeautifulSoup(data, "html.parser") for x in soup.findAll(tag, kwargs): for key in kwargs: # print(key) #>>class x.attrs.pop(key, None) # attrs: to access the actual dict #del x[key] would work also but will throw a KeyError if no key print(soup) return soup data=removeAttrib(data,"p",{"class":"story"})

提交回复
热议问题