我有一个HTML页面,我想使用python脚本进行编辑。我使用的是Dominate库
下面是一个简单的例子。
<html>
<head>
<title>asdjasda</title>
</head>
<body>
<h1>THIS IS A TEST</h1>
</body>
</html>简单的HTML,对吧?
以下是python脚本:
import dominate
from dominate.tags import *
page = open('index.html','r',encoding='utf-8')
with page.head:
link(rel='stylesheet', href='tts.css')
page.close()当我运行这个脚本时,我得到了以下错误。
Traceback (most recent call last):
File "script.py", line 6, in <module>
with page.head:
AttributeError: '_io.TextIOWrapper' object has no attribute 'head'我的HTML确实有一个“头”。
如何使用dominate编辑我的文件?
发布于 2017-08-13 19:15:53
原因是open()函数返回的内容没有head属性。
您应该使用Dominate库中的document。
试试这个:
page = open('index.html','r',encoding='utf-8')
page_str = page.read()
doc = dominate.document(page_str)
with doc.head:
link(rel='stylesheet', href='tts.css')
print(doc)希望它能帮上忙!
https://stackoverflow.com/questions/45659712
复制相似问题