Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45824

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

Date 2013-05-23 17:53 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Accessing Json data (I think I am nearly there) complete beginner
References <509030d9-08fd-40b2-8a89-cd4ecfad7a1a@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2023.1369328186.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 23/05/2013 17:09, Andrew Edwards-Adams wrote:
> Hey guys
> I think its worth stating that I have been trying to code for 1 week.
> I am trying to access some Json data. My innitial code was the below:
>
> "import mechanize
> import urllib
> import re
>
> def getData():
>      post_url = "http://www.tweetnaps.co.uk/leaderboards/leaderboard_json/all_time"
>      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' : '10',
>                    'sortname' : 'total_pl',
>                    'sortorder' : 'desc'}
>      #Encode the parameters
>      data = urllib.urlencode(parameters)
>      trans_array = browser.open(post_url,data).read().decode('UTF-8')
>
>      #print trans_array
>
>      myfile = open("test.txt", "w")
>      myfile.write(trans_array)
>      myfile.close()
>
> getData()
>
> raw_input("Complete")"
>
> I was recommended to use the following code to access the Json data directly, however I cannot get it to return anything. I think the guy that recommended me this method must have got something wrong? Or perhaps I am simply incompetent:
>
> 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')
>
>      text1 = json.loads(trans_array)
>      print text1['rows'][0]['id']  #play around with these values to access different data..
>
> getData()
>
> He told me to "#play around with these values to access different data.." really cant get anything out of this, any ideas?
>
> Many thanks AEA
>
I've just tried it. It prints "1048".

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Accessing Json data (I think I am nearly there) complete beginner Andrew Edwards-Adams <aeaprog@gmail.com> - 2013-05-23 09:09 -0700
  Re: Accessing Json data (I think I am nearly there) complete beginner Andrew Berg <bahamutzero8825@gmail.com> - 2013-05-23 11:40 -0500
    Re: Accessing Json data (I think I am nearly there) complete beginner Andrew Edwards-Adams <aeaprog@gmail.com> - 2013-05-23 09:58 -0700
      Re: Accessing Json data (I think I am nearly there) complete beginner Andrew Berg <bahamutzero8825@gmail.com> - 2013-05-23 12:59 -0500
      Re: Accessing Json data (I think I am nearly there) complete beginner Andrew Berg <bahamutzero8825@gmail.com> - 2013-05-23 13:11 -0500
        Re: Accessing Json data (I think I am nearly there) complete beginner Andrew Edwards-Adams <aeaprog@gmail.com> - 2013-05-23 11:19 -0700
          Re: Accessing Json data (I think I am nearly there) complete beginner Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-05-23 19:56 +0100
            Re: Accessing Json data (I think I am nearly there) complete beginner Andrew Edwards-Adams <aeaprog@gmail.com> - 2013-05-23 14:30 -0700
  Re: Accessing Json data (I think I am nearly there) complete beginner MRAB <python@mrabarnett.plus.com> - 2013-05-23 17:53 +0100

csiph-web