Python 一段时间后不需要的退出:错误可能';t将str转换为float

Python 一段时间后不需要的退出:错误可能';t将str转换为float,python,linux,nohup,Python,Linux,Nohup,我写了一个小脚本,每小时从一个网站上阅读并搜索某个字符串。这个字符串是我要计算的数字 如果我用“nohup python3/path/to/script&”运行脚本,它会运行一段时间。几个小时后,有时几天,甚至几天,脚本停止处理“~/nohup”中的错误输出,即float()无法转换第41行中的字符串 line 41: current_value = float(html_content[temperature_pos_begin:temperature_pos_end]) 整个脚本:使用异常

我写了一个小脚本,每小时从一个网站上阅读并搜索某个字符串。这个字符串是我要计算的数字

如果我用“nohup python3/path/to/script&”运行脚本,它会运行一段时间。几个小时后,有时几天,甚至几天,脚本停止处理“~/nohup”中的错误输出,即float()无法转换第41行中的字符串

line 41: current_value = float(html_content[temperature_pos_begin:temperature_pos_end])

整个脚本:

使用异常处理程序查看导致错误的原因。如果温度不可用,他们可能会使用某种占位符

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

什么字符串不能转换?将其放入
try
块,以便捕获异常,然后打印出错误字符串。您应该添加一些验证,以确保
temperature\u pos\u begin
变量确实具有有效值。如果没有,也许您应该记录捕获的HTML,以便跟踪出了什么问题。