我需要使用beautifulsoup和python为标记添加async属性。
鉴于此:
<script type="text/javascript" src="bootstrap.min.js" ></script>
我需要得到这个:
<script async type="text/javascript" src="bootstrap.min.js" ></script>
我正在尝试这个:
newTag.attrs['async'] = ''
但结果是:
<script async="" type="text/javascript" src="bootstrap.min.js" ></script>
任何帮助都非常感激。
答案 0 :(得分:4)
尝试使用newTag.attrs['async'] = None
:
from urllib import request
f = request.urlopen("http://www.example.com")
s = f.read()
f.close()
from bs4 import BeautifulSoup
soup = BeautifulSoup(s, "lxml")
newTag = soup.find("meta", charset = "utf-8")
tagCopy = newTag
newTag.attrs['async'] = ""
print(newTag)
tagCopy.attrs['async'] = None
print(tagCopy)
这会产生以下输出:
<meta async="" charset="utf-8"/>
<meta async charset="utf-8"/>