i'm trying build small script go through etsy api , retrieve information. api returns 25 different listing in json , appreciate if me learn how handle 1 @ time.
here example of json i'm dealing with:
{"count":50100,"results":[{"listing_id":114179207,"state":"active"},{"listing_id":11344567,"state":"active"}, and on.
is there simple way handle 1 of these listings @ time minimize amount of calls must make api?
here of code of how i'm dealing 1 when limit results returned 1:
r = requests.get('http://openapi.etsy.com/v2/listings/active?api_key=key&limit=1&offset='+str(offset_param)+'&category=clothing') raw_json = r.json() encoded_json = json.dumps(raw_json) dataobject = json.loads(encoded_json) if dataobject["results"][0]["quantity"] > 1: if dataobject["results"][0]["listing_id"] not in already_done: already_done.append(dataobject["results"][0]["listing_id"]) s = requests.get('http://openapi.etsy.com/v2/users/'+str(dataobject["results"][0]["user_id"])+'/profile?api_key=key') raw_json2 = s.json() encoded_json2 = json.dumps(raw_json2) dataobject2 = json.loads(encoded_json2) t = requests.get('http://openapi.etsy.com/v2/users/'+str(dataobject["results"][0]["user_id"])+'?api_key=key') raw_json3 = t.json() encoded_json3 = json.dumps(raw_json3) dataobject3 = json.loads(encoded_json3)
seeing how results field (or key) contains list structure, can iterate through following
json_str = { ...other key-values, "results": [{"listing_id":114179207,"state":"active"},{"listing_id":11344567,"state":"active"}, ...and on] } results = json_str['results'] # gives list of dicts # iterate through list result in results: if result['state'] == 'active': do_something_with( result['listing_id'] else: do_someotherthing_with( result['listing_id'] # or none @
Comments
Post a Comment