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

前端 未结 2 379
走了就别回头了
走了就别回头了 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='<p class="story">xxx </p> <p id="2">yyy</p> <p class="story"> zzz</p>'
    
    
    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"})
    
    0 讨论(0)
  • 2021-01-23 11:49

    You can pass a dictionary instead:

    from bs4 import BeautifulSoup
    data='<p class="story">xxx </p> <p id="2">yyy</p> <p class="story"> zzz</p>'
    soup = BeautifulSoup(data, "html.parser")
    
    def removeAttrib(soup, tag, argdict):
    
        for x in soup.findAll(tag, argdict):
            x.decompose()
    
    removeAttrib(soup, "p", {"class": "story"})
    
    0 讨论(0)
提交回复
热议问题