I'm scrapping Google API and I'm having fun so far.
What doesn't make me sleep at night is code that sometime works and sometimes no, like this one:
Here the code if you want to copy/paste it:
if response_data2['status'] == 'OK':
Googleplace_id = response_data2['result']['place_id']
Googleid = response_data2['result']['id']
GoogleName = response_data2['result']['name']
for types in response_data2['result']['address_components']:
field = types.get('types', [])
if 'street_number' in field:
GoogleStreet_Number = types['long_name']
if 'route' in field:
GoogleStreet = types['long_name']
if 'postal_code' in field:
GooglePostal_Code = types['long_name']
if 'locality' in field:
GoogleCity = types['long_name']
if 'administrative_area_level_1' in field:
GoogleArea1 = types['long_name']
if 'administrative_area_level_2' in field:
GoogleArea2 = types['long_name']
else:
GoogleArea2 = None
if 'country' in field:
GoogleCountry = types['long_name']
if 'country' in field:
GoogleCountryCode = types['short_name']
if 'international_phone_number' in response_data2['result']:
GooglePhone = response_data2['result']['international_phone_number']
else:
GooglePhone = None
GoogleLatitude = response_data2['result']['geometry']['location']['lat']
GoogleLongitude = response_data2['result']['geometry']['location']['lng']
GoogleTypes = json.dumps(response_data2['result']['types'])
if 'rating' in response_data2['result']:
GoogleRating = response_data2['result']['rating']
else:
GoogleRating = None
GoogleURL = response_data2['result']['url']
if 'website' in response_data2['result']:
GoogleWebsite = response_data2['result']['website']
else:
GoogleWebsite = None
time.sleep(2)
Basically what I think it's going on here is that for mysterious reason if I scrap Google API JSON with an if/else it works fine but not if the same if/else is inside a loop.
For example the if/else is working for the 'international_phone_number':
if 'international_phone_number' in response_data2['result']:
GooglePhone = response_data2['result']['international_phone_number']
else:
GooglePhone = None
But the same if/else logic doesn't work for 'administrative_area_level_2' variable
if 'administrative_area_level_2' in field:
GoogleArea2 = types['long_name']
else:
GoogleArea2 = None
And as result I have a NULL in the database when the phone number is not there, which is right, but ONLY NULLs for Area2
Please tell me I'm crazy.
Aucun commentaire:
Enregistrer un commentaire