0

再起動後にシームレスに再開するために、Web ページに変更が加えられたときに通知し、ページの現在の状態をファイルに保存する Python スクリプトを作成しています。コードは次のとおりです。

import urllib
url="http://example.com"
filepath="/path/to/file.txt"
try:
    html=open(filepath,"r").read() # Restores imported code from previous session
except:
    html="" # Blanks variable on first run of the script
while True:
    imported=urllib.urlopen(url)
    if imported!=html:
    # Alert me
    html=imported
    open(filepath,"w").write(html)
# Time delay before next iteration

スクリプトを実行すると、以下が返されます。

Traceback (most recent call last):
  File "April_Fools.py", line 20, in <module>
    open(filepath,"w").write(html)
TypeError: expected a character buffer object

------------------
(program exited with code: 1)
Press return to continue

これが何を意味するのかわかりません。私はPythonに比較的慣れていません。どんな助けでも大歓迎です。

4

2 に答える 2

1

urllib.urlopen文字列を返さず、ファイルのようなオブジェクトとして応答を返します。その応答を読む必要があります。

html = imported.read()

そうして初めて文字列をhtmlファイルに書き込むことができます。

于 2013-03-31T18:53:25.133 に答える