我請求一個Ajax Web站點Python腳本和獲取城市和http://www.yurticikargo.com/bilgi-servisleri/Sayfalar/en-yakin-sube.aspx獲取阿賈克斯JSON輸出與Python請求圖書館
分支機構我完成了第一步與發佈 {cityID: 34}
到這個網址和FETC的JSON輸出。
但我不能與Python retrive的JSON輸出,雖然我的Chrome高級休息客戶擴展獲得成功地,張貼{cityID:54,townID:5416,unitOnDutyFlag:null,closestFlag:2}
所有的源代碼是在這裏
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
class Yurtici(object):
baseUrl = 'http://www.yurticikargo.com/'
ajaxRoot = '_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-sswservices.aspx/'
getTown = 'GetTownByCity'
getUnit = 'GetUnit'
urlGetTown = baseUrl + ajaxRoot + getTown
urlGetUnit = baseUrl + ajaxRoot + getUnit
headers = {'content-type': 'application/json','encoding':'utf-8'}
def __init__(self):
pass
def ilceler(self, plaka=34): # Default testing value
payload = {'cityId':plaka}
url = self.urlGetTown
r = requests.post(url, data=json.dumps(payload), headers=self.headers)
return r.json() # OK
def subeler(self, ilceNo=5902): # Default testing value
# 5902 Çerkezköy
payload= {'cityID':59,'townID':5902,'unitOnDutyFlag':'null','closestFlag':0}
url = self.urlGetUnit
headers = {'content-type': 'application/json','encoding':'utf-8'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.status_code, r.raw.read()
if __name__ == '__main__':
a = Yurtici()
print a.ilceler(37) # OK
print a.subeler() # NOT OK !!!
這是一個小問題,但[文檔明確指出](http://docs.python-requests.org/en/latest/user/quickstart/#raw-response-content)你不應該使用' Response.raw',除非你在原始請求中設置了'stream = True'。 – Lukasa