'NoneType'对象没有属性'attrs' - 如果没有值=跳过该函数

时间:2018-04-21 17:09:27

标签: python if-statement nonetype

所以我一直试图找出如何解决我一直困扰的问题 - 基本上我想做的是每当值上有“NoneType”时跳过该函数,这意味着如果有的话没有价值,然后只是跳过剩下的代码。

所以我试图得到的是

bs4.find("div", {'class': "clock"}).attrs['data-code']

意味着有时这个函数不能一直工作所以我试着基本上做 - 如果它没有任何值,那么只需继续其余的代码 - Else excute it - 我所做的就是

 if bs4.find("div", {'class': "clock"}).attrs['data-code'] == None:
                 log("Does it work?")
                 gettimer = bs4.find("div", {'class': "clock"}).attrs['data-code']
                 dothemath = int(gettimer) - 189386
                 releasetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(dothemath))

我现在得到的问题是,只要它到达if语句就会停止,因为它无法找到值并且它会自动停在那里 - 只要有NoneType就可以这样做,只需跳过其余部分码?

1 个答案:

答案 0 :(得分:1)

使用using System; using System.Diagnostics; using System.Threading; class Program { static void Main(string[] args) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Thread.Sleep(10000); stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); } }

try/except
相关问题