How to pass item in loop

This is the place for queries that don't fit in any of the other categories.

How to pass item in loop

Postby digistam » Wed Mar 13, 2013 6:37 pm

I'm working on a script to query 'paged' results. More specific, the total output (in JSON format) is divided in batches or 'pages' of 20 results and a nextpage token is needed to get the next 20 results (or 'next page'). As long as there are more results after the initial 20 results, the output contains a nextpage token (except the last batch of 20 results, in that case the nextpage token is empty)

Output example:

first 20 results:
{ nextpage: xxxx, items: { item a, item b, item c } }
next 20 results:
{ nextpage: yyyy, items: { item d, item e, item f } }
next 20 results:
{ nextpage: zzzz, items: { item g, item h, item i } }
next 20 results:
{ nextpage: , items: { item a, item b, item c } }

Now I want to loop through the 'pages' and stop after the last one, for example:

Code: Select all
import urllib2
url = urllib2.Request('https://etc. etc..json&nextpage=xxxx)
myjson = json.load(urllib2.urlopen(url))
row = []
while len(myjson['nextpage']) != 0:
     for item in myjson['items']:
          row.append(etc. etc.)


I know my code is crap and it clearly don't work, how can I pass the nextpage token of the first loop (let's say it is yyyy after the first loop and zzzz after the second loop) and use it as a parameter in the next loop of the current script run ?
digistam
 
Posts: 1
Joined: Wed Mar 13, 2013 6:17 pm

Re: How to pass item in loop

Postby joneshf » Thu Mar 14, 2013 12:54 pm

Convert your while loop to a for loop.
joneshf
 
Posts: 8
Joined: Thu Feb 07, 2013 3:48 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baldyr, Google [Bot], Yoriz and 1 guest

cron