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

前端 未结 2 382
走了就别回头了
走了就别回头了 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:49

    You can pass a dictionary instead:

    from bs4 import BeautifulSoup
    data='

    xxx

    yyy

    zzz

    ' soup = BeautifulSoup(data, "html.parser") def removeAttrib(soup, tag, argdict): for x in soup.findAll(tag, argdict): x.decompose() removeAttrib(soup, "p", {"class": "story"})

提交回复
热议问题