0

So I have been trying to find out how to solve a issue that I have been stuck on - Basically what I want to do is that whenever there is a "NoneType" on a value = Skip that function so meaning if there is no value then just skip the rest of the code.

So what I have tried to get is

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

meaning that sometimes this function is not working all the time so I tried to basically do - If there is no value from it then just continue the rest of the code - Else excute it - what I have done is

 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))

The issue im getting now that it stops whenever it reach to the if statement because it can't find the value and it automatic stops there - What can I do so whenever there is NoneType on it, Just skip the rest of the code?

CDNthe2nd
  • 369
  • 1
  • 5
  • 19
  • Maybe this: https://stackoverflow.com/questions/4978738/is-there-a-python-equivalent-of-the-c-sharp-null-coalescing-operator – Barmar Apr 21 '18 at 17:12
  • Hmm not really I think, Because whenever I try to find it on bs4 - it automatic exits which means I can't continue do the rest of the code :) – CDNthe2nd Apr 21 '18 at 17:14
  • You need to have a default object that supports the same methods. Then write `(bs4.find("div", {'class': "clock"}) or defaultObj).attrs['data-code']` – Barmar Apr 21 '18 at 17:17
  • You could also put your code in a `try/except`. – Barmar Apr 21 '18 at 17:17
  • Hmm I don't really understand! Could you please give me an example! default objects such as? :O – CDNthe2nd Apr 21 '18 at 17:18

1 Answers1

1

Use try/except:

try:
    gettimer = bs4.find("div", {'class': "clock"}).attrs['data-code']
    // rest of code
except:
    pass
Barmar
  • 741,623
  • 53
  • 500
  • 612