2014-04-26 59 views
0

我,給了我下面的JSON字符串,如果我打他們的瀏覽器的URL -如何從請求庫中使用Python腳本從url獲取json數據?

下面是我的網址,讓我們說這是URL-A,我必須圍繞三個網址 -

http://hostnameA:1234/Service/statistics?%24format=json 

而且下面是我的JSON字符串 -

{ 
"description": "", 
"statistics": { 
    "dataCount": 0, 
} 
} 

現在我已經寫它掃描我的所有3網址,然後再解析JSON字符串從中提取的dataCount值的Python腳本。它應該每隔幾秒繼續運行一次以掃描URL並解析它。

下面是我的URL

hostnameA  http://hostnameA:1234/Service/statistics?%24format=json 
hostnameB  http://hostnameB:1234/Service/statistics?%24format=json 
hostnameC  http://hostnameC:1234/Service/statistics?%24format=json 

而且我希望看到的數據是這樣的控制檯上,這裏dataCount將實際數量

hostnameA - dataCount 
hostnameB - dataCount 
hostnameC - dataCount 

和我有以下的Python腳本在我的cygwin本地工作正常,但如果我在我公司的生產ubuntu機器上運行它,它會給出錯誤 -

import requests 
from time import sleep 

def get_data_count(url): 
    try: 
     req = requests.get(url) 
    except requests.ConnectionError: 
     return 'could not get page' 

    try: 
     # this line is giving an error 
     return int(req.json['statistics']['dataCount']) 
    except TypeError: 
     return 'field not found' 
    except ValueError: 
     return 'not an integer' 

def main(): 
    urls = [ 
     ('hostnameA', 'http://hostnameA:1234/Service/statistics?%24format=json'), 
     ('hostnameB', 'http://hostnameB:1234/Service/statistics?%24format=json'), 
     ('hostnameC', 'http://hostnameC:1234/Service/statistics?%24format=json') 
    ] 

    while True: 
     print('') 
     for name, url in urls: 
      res = get_data_count(url) 
      print('{name} - {res}'.format(name=name, res=res)) 
     sleep(10.) 

if __name__=="__main__": 
    main() 

下面是我得到的錯誤 -

AttributeError: 'Response' object has no attribute 'json' 

我使用Python 2.7.3和Ubuntu 12.04和requests我運行的版本是0.8.2(我猜這就是問題所在)。

在任何情況下,有沒有什麼辦法可以使用除requests以外的其他庫來重寫上述腳本,這意味着只從服務器獲取數據的部分,我們可以使用其他庫嗎?

因爲我猜,我不能更新這個包,因爲它是我們的生產Ubuntu服務器,所以我需要找到其他方式來做到這一點。

+0

事實上,'requests' 0.8.2是古老的,不支持'.json'。你可以在virtualenv中安裝一個* newer *版本,然後用它來代替。 –

+0

@MartijnPieters:謝謝,但我該如何安裝'virutualenv'?我以前從未使用過它。如果你能指導我,那麼它會有很大的幫助。不是Python專家:( – AKIWEB

+0

有一個[Ubuntu軟件包](http://packages.ubuntu.com/precise/python-virtualenv);這是針對Precise Pangolin,Ubuntu服務器版本的(根據0.8.2 'requests'的版本標籤) –

回答

2

您可以使用requests還在,只是不能依賴響應對象爲你做解碼:

import json 

# ... 
data = json.loads(req.content) 
return int(data['statistics']['dataCount']) 

版本0.8.2是古代;您可以使用virtualenv創建一個位置爲你安裝一個新版本,而不是:

$ virtualenv venv 
New python executable in venv/bin/python2.7 
Also creating executable in venv/bin/python 
Installing Setuptools..............................................................................................................................................................................................................................done. 
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done. 
$ cd venv/ 
$ bin/pip install requests 
Downloading/unpacking requests 
    Downloading requests-2.2.1.tar.gz (421kB): 421kB downloaded 
    Running setup.py egg_info for package requests 

Installing collected packages: requests 
    Running setup.py install for requests 

Successfully installed requests 
Cleaning up... 
$ bin/python -c 'import requests; print requests.__version__' 
2.2.1 
+0

它可以很好地處理你的第一個建議。真棒,謝謝:) – AKIWEB

+0

感謝你讓我知道如何使用'virtualenv'。 – AKIWEB