First, I'm going to talk about how you iterate over your results. Based on your code, xmlload1['rows']
is an array of dicts, so instead of choosing an arbitrary number, you can iterate over it directly instead. To make this a better example, I'm going to set up some arbitrary data to make this clear:
xmlload1 = {
"rows": [{"cell": {"username": "one", "race_horse":"b"}}, {"cell": {"username": "two", "race_horse": "c"}}]
}
So, given the data above, you can just iterate through rows in a for loop, like this:
for row in xmlload1['rows']:
cell = row["cell"]
print cell["username"]
print cell["race_horse"]
Each iteration, cell takes on the value of another element in the iterable (the list in xmlload1['rows']
). This works with any container or sequence that supports iteration (like lists
, tuples
, dicts
, generators
, etc.)
Notice how I haven't used any magic numbers anywhere, so xmlload1['rows']
could be arbitrarily long and it would still work.
You can set the requests to be dynamic by using a function, like this:
def get_data(rp=8, page=1):
parameters = {'page' : str(page),
'rp' : str(rp),
'sortname' : 'roi',
'sortorder' : 'desc'
}
data = urllib.urlencode(parameters)
trans_array = browser.open(post_url,data).read().decode('UTF-8')
return json.loads(trans_array)
Now, you can call get_data(rp=5)
to get 5 rows, or get_data(rp=8)
to get 8 rows [and get_data(rp=8, page=3)
to get the third page], etc. And you can clearly add additional variables or even pass in the parameters
dict to the function directly.