Home > Mobile >  How to remove onclick attribute from tag using BeautifulSoup
How to remove onclick attribute from tag using BeautifulSoup

Time:08-20

Here is my code:

<h1 onclick="alert('Hello')">Click</h1>

I want to do this:

<h1>Click</h1>

CodePudding user response:

Solution:

from bs4 import BeautifulSoup


html = """<h1 onclick="alert('Hello')">Click</h1>"""

a = BeautifulSoup(html,'html.parser')

for tag in a.find_all():
    if 'onclick' in tag.attrs:
        tag.attrs.pop('onclick')

print(a)
#<h1>Click</h1>

CodePudding user response:

I think you want something as follows:

from bs4 import BeautifulSoup

html = '<h1 onclick="alert(\'Hello\')">Click</h1>'
soup = BeautifulSoup(html)
print(soup)
# <html><body><h1 onclick="alert('Hello')">Click</h1></body></html>

# get h1
h1 = soup.h1

print(h1.attrs)
# {'onclick': "alert('Hello')"}

# so, we simply want to reset the attrs to an empty dict:
h1.attrs = {}

print(h1)
# <h1>Click</h1>
  • Related