Accessing Json data (I think I am nearly there) complete beginner

Andrew Edwards-Adams aeaprog at gmail.com
Thu May 23 12:58:17 EDT 2013


On Thursday, May 23, 2013 5:40:49 PM UTC+1, Andrew Berg wrote:
> On 2013.05.23 11:09, Andrew Edwards-Adams wrote:
> 
> > I was recommended to use the following code to access the Json data directly, however I cannot get it to return anything.
> 
> Where exactly is the problem? Do you not get JSON back? Do you get the wrong values? Do you get a KeyError or IndexError trying to get
> 
> values from text1? Are there gremlins going around flipping bits in memory? It's good that you posted code, but "really cant get anything
> 
> out of this" isn't very useful.
> 
> -- 
> 
> CPython 3.3.2 | Windows NT 6.2.9200 / FreeBSD 9.1

Hi thanks for the reply Andrew, my first bit of code was heading in the right direction I was managing to pull out the usernames from the JSON, using REGEX. I was told that the second piece of code is the more efficient way to pull data from the JSON. When I say it doesnt do anything I can adjust the areas suggested. But it returns nothing, I just get the "= Restart =" line appear in the python shell. If there was a trackback/debug I might know where to look, but its not yielding errors, its simply yielding nothing. What i dont know, is if it is the code that isnt working, or what I am inputting in the " print text1['rows'][0]['id']" that isnt working.


Since this code: 

import mechanize
import urllib
import json
def getData():  
    post_url = "http://www.tweetnaps.co.uk/leaderboards/leaderboard_json/current_week"
    browser = mechanize.Browser()
    browser.set_handle_robots(False)
    browser.addheaders = [('User-agent', 'Firefox')]
 
    #These are the parameters you've got from checking with the aforementioned tools
    parameters = {'page' : '1',
                  'rp' : '50',
                  'sortname' : 'total_pl',
                  'sortorder' : 'desc'
                 }
    #Encode the parameters
    data = urllib.urlencode(parameters)
    trans_array = browser.open(post_url,data).read().decode('UTF-8')

 appears in both examples and my first attempt is definitely returning data from the JSON, I can only assume that the error is in the last two lines of his code, or as stated my imputing incorrect parameters.

:S



More information about the Python-list mailing list