import time
import traceback
import sys
import tools
from BeautifulSoup import BeautifulSoup
f = open("randomwords.txt","w")
while 1:
try:
page = tools.download("http://wordnik.com/random")
soup = BeautifulSoup(page)
si = soup.find("h1")
w = si.string
print w
f.write(w)
f.write("\n")
time.sleep(3)
except:
traceback.print_exc()
continue
f.close()
它打印得很好。它只是不会写入文件。它是0字节。为什么我的脚本不能写入文件?
[`除外:`是邪恶的(http://docs.python.org/howto/doanddont。 HTML#除外)。除非有充分的理由和解释原因的评论,否则也会吞咽异常。而*真正的*无限循环(没有'break'或异常发生的类型)是......好,是一个问题;) – delnan 2011-02-13 19:57:12