jeudi 26 février 2015

Could not Get the Output with Python Requests from a website

i am a newbie in python and trying to use request.


The web site i would like to get data is :


http://ift.tt/1MUARz3


I can get what I want from terminal by typing :



curl 'http://ift.tt/1MUARz5' --data 'cityId=3969&cmd=districts'


I would like to get the data for instance for one city and then for every district. But first i want to get every disrict for each city. I tried with that code and the status code is 200 but I receive an error saying Json object couldnt be decoded. I think i need to figured it out how to construct cityId=3969&cmd=districts and then get whats inside. Also the encoding is gzip.


The code so far i wrote shown below:



import requests
import json


class Mortgage(object):

urlGetTown = 'http://ift.tt/1MUARz5'

headers = {'content-type': 'text/html'}

def __init__(self):
pass

def ilceler(self, plaka=3969): # Default testing value
payload = {'cityId':plaka, 'cmd':'districts'}
url = self.urlGetTown
r = requests.post(url, data=json.dumps(payload), headers=self.headers)
return r.status_code # OK


if __name__ == '__main__':
a = Mortgage()
print a.ilceler()

Aucun commentaire:

Enregistrer un commentaire