Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python 3.5.2和Dominate编辑HTML页面_Python_Html_Python 3.5_Dominate - Fatal编程技术网

使用python 3.5.2和Dominate编辑HTML页面

使用python 3.5.2和Dominate编辑HTML页面,python,html,python-3.5,dominate,Python,Html,Python 3.5,Dominate,我有一个HTML页面,我想用python脚本编辑它。我正在使用图书馆 下面是一个简单的例子 <html> <head> <title>asdjasda</title> </head> <body> <h1>THIS IS A TEST</h1> </body> </html> 运行此脚本时,我遇到以下错误 Traceback (most recent call las

我有一个HTML页面,我想用python脚本编辑它。我正在使用图书馆

下面是一个简单的例子

<html>
<head>
  <title>asdjasda</title>
</head>
<body>
  <h1>THIS IS A TEST</h1>
</body>
</html>
运行此脚本时,我遇到以下错误

Traceback (most recent call last):  
  File "script.py", line 6, in <module>  
    with page.head:  
AttributeError: '_io.TextIOWrapper' object has no attribute 'head'
回溯(最近一次呼叫最后一次):
文件“script.py”,第6行,在
使用page.head:
AttributeError:“\u io.TextIOWrapper”对象没有属性“head”
我的HTML确实有一个“头”


如何使用dominate编辑文件?

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

您应该使用主控库中的
文档

试试这个:

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)

希望有帮助

它确实有效,但由于某种原因,它会把整个文档弄乱。此外,它没有对原始HTML文件进行任何更改,而是将我在
title
tagDominate中的h1标记放在了主体中,而不是HTML解析器。此代码将原始html转储到新支配文档的title参数中。支配不是html解析器。它严格用于创建新文档,而不是解析现有的html文件。你需要像@Knio-Yup这样的东西。。。经过几次尝试后,我放弃了主宰,转而开始使用BeautifulSoup。有点困惑地了解到,有Python包(这是一个伟大的包IMHO)和Nodejs包,这是一个解析器。
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)