2013-11-23 49 views
0

我請求一個Ajax Web站點Python腳本和獲取城市和http://www.yurticikargo.com/bilgi-servisleri/Sayfalar/en-yakin-sube.aspx獲取阿賈克斯JSON輸出與Python請求圖書館

分支機構我完成了第一步與發佈 {cityID: 34}到這個網址和FETC的JSON輸出。

http://www.yurticikargo.com/_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-sswservices.aspx/GetTownByCity

但我不能與Python retrive的JSON輸出,雖然我的Chrome高級休息客戶擴展獲得成功地,張貼{cityID:54,townID:5416,unitOnDutyFlag:null,closestFlag:2}

http://www.yurticikargo.com/_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-unitservices.aspx/GetUnit

所有的源代碼是在這裏

#!/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 !!! 
+0

這是一個小問題,但[文檔明確指出](http://docs.python-requests.org/en/latest/user/quickstart/#raw-response-content)你不應該使用' Response.raw',除非你在原始請求中設置了'stream = True'。 – Lukasa

回答

2

您的代碼沒有發佈到您使用的網址你的文字示例。

讓我們走過這個倒退。首先,讓我們看看失敗的POST。

url = self.urlGetUnit 
headers = {'content-type': 'application/json','encoding':'utf-8'} 
r = requests.post(url, data=json.dumps(payload), headers=headers) 

因此,我們發佈到URL等於self.urlGetUnit。好吧,讓我們來看看這是如何定義的:

baseUrl = 'http://www.yurticikargo.com/' 
ajaxRoot = '_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-sswservices.aspx/' 
getUnit = 'GetUnit' 
urlGetUnit = baseUrl + ajaxRoot + getUnit 

如果你在urlGetUnit的工作,你得到的網址爲http://www.yurticikargo.com/_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-sswservices.aspx/GetUnit。讓我們把這個和你在Chrome中使用的URL一起來比較差異:

http://www.yurticikargo.com/_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-sswservices.aspx/GetUnit 
http://www.yurticikargo.com/_layouts/ArikanliHolding.YurticiKargo.WebSite/ajaxproxy-unitservices.aspx/GetUnit 

看到區別?這兩個網址的ajaxRoot不一樣。排序出來,你會得到一個JSON響應。

+0

非常感謝,我困了:) 現在我解決了它。通過修復ajax網址。此外,該響應被編碼爲gzip。我使用zlib解壓縮,如下所示: http://stackoverflow.com/questions/2695152/in-python-how-do-i-decode-gzip-encoding – guneysus

+1

請求可以爲你做。使用'Response.content','Response.text'或'Response.json()'和Requests會自動解壓縮它。 – Lukasa

+0

我已經使用'return r.json()',謝謝。 – guneysus