0

我有一个 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 确实有一个“头”。

如何使用主宰来编辑我的文件?

4

1 回答 1

-1

原因是open()函数返回的没有属性head

您应该document从 Dominate 库中使用。

尝试这个:

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)

希望能帮助到你!

于 2017-08-13T11:15:52.770 回答