2021年5月3日星期一

Key Error Handling in Python while trying to get info from websites

Python newbie here. I have a small script which I use to get coin prices from different websites. I created a .text file to store coin names and I am using that in my code.

# paribu          print("paribu ->")          paribu_response = requests.get('https://www.paribu.com/ticker')          symbol_paribu = coin_list[i] + "_TL"    #I am storing coin names in this coin_list file          symbol_usdt_tl = "USDT_TL"          paribu_dic = json.loads(paribu_response.content)          if "lowestAsk" in paribu_dic[symbol_paribu]:              paribu_bid_price = paribu_dic[symbol_paribu]['highestBid']/ paribu_dic[symbol_usdt_tl]['lowestAsk']   #this is the line that I am getting error              print("paribu_bid_price "+str(paribu_bid_price))              paribu_ask_price = paribu_dic[symbol_paribu]['lowestAsk'] / paribu_dic[symbol_usdt_tl]['lowestAsk']              print("paribu_ask_price " + str(paribu_ask_price))              if (max_bid_val < paribu_bid_price):                  max_bid_val = paribu_bid_price                  max_bid_place = "paribu"              if (min_ask_val > paribu_ask_price):                  min_ask_val = paribu_ask_price                  min_ask_place = "paribu"          else:              print(coin_list[i] + " not in paribu") #if that individual coin is not listed in this website I should get "xxx not in paribu" instead of key error            if is_run == False:              break  

The thing is if this particular website supports that individual coin everything goes well, but if it is not listed in that website I am getting a "Key Error".

Traceback (most recent call last):    File "C:\python\lib\threading.py", line 892, in run      self._target(*self._args, **self._kwargs)    File "C:/Users/HasanC/Desktop/0502/0502.py", line 127, in start_checking      if "lowestAsk" in paribu_dic[symbol_paribu]:  KeyError: 'ENJ_TL'  
https://stackoverflow.com/questions/67377879/key-error-handling-in-python-while-trying-to-get-info-from-websites May 04, 2021 at 10:04AM

没有评论:

发表评论