Python: parse JSON in loop

这一生的挚爱 提交于 2019-12-12 02:44:46

问题


I have a function that pick-ups Country,City,Latitude,Longitude from a database and search on Yelp API for particular business.

And everything works fine:

def get_movietheaters_for(country, city, latitude, longitude):
    connection2 = pyodbc.connect('DRIVER={SQL Server};'                      
                                 'SERVER=ASPIRES3;'                         
                                 'DATABASE=worldcitiespop;'                 
                                 'UID=sqlninja;'                            
                                 'PWD=sqlninja')                            
    cursor2 = connection2.cursor()                                          
    # Call Yelp API to pull business data                                   
    # (Yelp v3 API: https://nz.yelp.com/developers/documentation/v3)        
    url = 'https://api.yelp.com/v3/businesses/search'                       
    params = {'cc': country,                                                
              'location': city,                                             
              'cll': "%s,%s" % (latitude, longitude),                       
              'categories': args.category,                                  
              'limit': args.limit}                                          
    response = requests.get(url = url, headers = headers, params=params)    
    # if response.status_code == 200:                                       
    response_data = response.json()                                         

    sqlStatement = "INSERT INTO VistaYelp (ID, Name, City, Zip_code, Country, State, Address1, Address2, Address3, Latitude, Longitude, Phone, Yelp_URL, Review_Count, Rating) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" # Query
    # Here we go to store JSON elements for SQL


    for SQL_element in response_data['businesses']:
        SQL_ID = SQL_element['id']                                                      
        SQL_Name = SQL_element['name']                                                  
        SQL_City = SQL_element['location']['city']                                      
        SQL_Zip_code = SQL_element['location']['zip_code']                              
        SQL_Country = SQL_element['location']['country']                                
        SQL_State = SQL_element['location']['state']                                    
        SQL_Address1 = SQL_element['location']['address1']                              
        SQL_Address2 = SQL_element['location']['address2']                              
        SQL_Address3 = SQL_element['location']['address3']                              
        SQL_Latitude = SQL_element['coordinates']['latitude']                           
        SQL_Longitude = SQL_element['coordinates']['longitude']                         
        SQL_Phone = SQL_element['phone']                                                
        SQL_YelpURL = SQL_element['url']                                                
        SQL_Review = SQL_element['review_count']                                        
        SQL_Rating = SQL_element['rating']                                              

        if args.do == 'show':   
            print (SQL_ID,SQL_Name,SQL_City,SQL_Zip_code,SQL_Country,SQL_State,
                SQL_Address1,SQL_Address2,SQL_Address3,SQL_Latitude,SQL_Longitude,SQL_Phone)
        elif args.do == 'save':    
            cursor2.execute(sqlStatement, SQL_ID,SQL_Name,SQL_City,SQL_Zip_code,SQL_Country,SQL_State,SQL_Address1,SQL_Address2,SQL_Address3,SQL_Latitude,SQL_Longitude,SQL_Phone,SQL_YelpURL,SQL_Review,SQL_Rating)
            connection2.commit()

    if args.do == 'show':
        print ('\nTotal Cinemas found: ' , len(response_data['businesses']),' for Latitude and Longitude ', latitude, longitude)
    elif args.do == 'save':
        print ('\nTotal Cinemas found and saved in database: ' , len(response_data['businesses']),' for', latitude, longitude)

Problem start when the script search for a town that doesn't exists or is not in the Yelp database, so JSON call returns me:

{
  "error": {
    "code": "LOCATION_NOT_FOUND",
    "description": "Could not execute search, try specifying a more exact location."
  }
}

and the script dies in terrible pain (my pain actually):

And it make sense to me, Python is saying:

    for SQL_element in response_data['businesses']:
KeyError: 'businesses'

Which translated means:"Hey dude, you know 'businesses' JSON element, there is no such element in the response, so I don't know what to do, so I stop here."

How can I structure something like: keep doing your job and if response_data['error']['code'] == 'LOCATION_NOT_FOUND': do nothing?


回答1:


if 'businesses' in response_data:
   for SQL_element in response_data['businesses']:
       …



回答2:


response_data = response.json()

if response_data['error']['code'] == 'LOCATION_NOT_FOUND':
    # if nothing found, print a message and terminate the function
    print('No cinemas found.')
    return

# otherwise keep on going
sqlStatement = "INSERT INTO ..."



回答3:


response_data = response.json()
if response_data.get('error'):
  return  # Do nothing and return



回答4:


After response_data = response.json()

Verify if response_data Has a businesses key

If `businesses` in response_data.keys():
    # Do the code



回答5:


You should probably try to catch the error and abort further execution of the function.

if "error" in response_data:
    return #Function will stop on a return call

You could even return something like a status if you want, e.g.

return True 
return False

See this question for more information on the return command.



来源:https://stackoverflow.com/questions/44359686/python-parse-json-in-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!