2017-08-13 8 views
0

私はpythonスクリプトを使って編集したいHTMLページを持っています。私はDominateライブラリを使用していますPython 3.5.2とDominateを使ってHTMLページを編集する

ここはベアボーンの例です。

<html> 
<head> 
    <title>asdjasda</title> 
</head> 
<body> 
    <h1>THIS IS A TEST</h1> 
</body> 
</html> 

単純なHTMLの権利ですか?

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() 

私はこのスクリプトを実行すると、私は次のエラーを取得する:
はここでPythonスクリプトです。

Traceback (most recent call last): 
    File "script.py", line 6, in <module> 
    with page.head: 
AttributeError: '_io.TextIOWrapper' object has no attribute 'head' 

私のHTMLには「頭」があります。

私のファイルを支配する方法を教えてください。

答えて

0

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) 

はそれが役に立てば幸い!

+0

これは機能しますが、何らかの理由でドキュメント全体が壊れてしまいます。また、オリジナルのHTMLファイル – YaddyVirus

+0

に変更はありません。本体のh1タグを 'title'タグの内側に入れます。 – YaddyVirus

+0

そして、私のindex.htmlファイルをクリアしました – YaddyVirus

関連する問題