recursion - Error when using Requests with python 3.5 recursively (GooglePlaces API) -
i have been having problem try send request , if there next page token in result take link , execute request recursively until there no next page token in result. first request works fine when there next page token in response , tries execute new request result invalid response if take link given result , use in postman or on browser fine. i'm assuming has requests running on different threads @ same time.
the second response request using python:
{'html_attributions': [], 'status': 'invalid_request', 'results': []}
here have:
import requests def getplaces(location,radius,type, apikey): url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+location+"&radius="+radius+"&type="+type+"&key="+apikey print('getting results type ' + type + '...') r = requests.get(url) response = r.json() results = [] if response['status'] == 'zero_results': print("did not find results type "+type) else: print("results type "+type) result in response['results']: results.append(result) print(result) print('printing results') print(results) if 'next_page_token' in response: print("there next page") page_token = response['next_page_token'] print(page_token) next_results = getnextplace(page_token,apikey) print(next_results) results.append(next_results) return results # rest of results def getnextplace(page_token,apikey): print('...') next_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='+location+'&radius='+radius+'&type='+type+'&pagetoken=' + page_token + '&key=' + apikey print(next_url) r = requests.get(next_url) response = r.json() results = [] print(response) if response['status'] == 'zero_results': print("did not find results") elif response['status'] == 'invalid_request': print('invalid response') else: next_result in response['results']: results.append(next_result) print(next_result) if 'next_page_token' in response: new_page_token = response['next_page_token'] getnext = getnextplace(new_page_token,apikey) results.append(getnext) return results
figured out issue! google api doesn't allow consecutive requests api if last request within ~2 seconds. did have had program sleep 3 seconds , sent request. working fine
Comments
Post a Comment