我写了一个小脚本,每小时从网站上读取并搜索某个字符串。这个字符串是我想计算的数字。
如果我使用"nohup python3/path/to/script &"运行脚本,它可以工作一段时间。几个小时后,一些甚至弱化脚本停止处理"~/nohup"中的错误输出,即 float() 无法转换第 41 行中的字符串。
line 41: current_value = float(html_content[temperature_pos_begin:temperature_pos_end])
整个脚本:http://pastebin.com/AEY1Kafa
使用异常处理程序查看导致错误的原因。如果温度不可用,也许他们会使用某种占位符。
try:
current_value = float(html_content[temperature_pos_begin:temperature_pos_end])
except ValueError:
print "Failed to convert %r to a float"%html_content[temperature_pos_begin:temperature_pos_end]
current_value = None # or something that makes sense