簡體   English   中英

使用Python獲取API

[英]Getting API's using Python

我一直在嘗試從BTC-E價格API獲取價格,例如,我不能僅指定price[109:116] 因為如果發生這種情況,它只會以錯誤的格式打印2個數字。 我只需要掌握“ last:”之后的內容即可。

from urllib2 import Request, urlopen, URLError

def btceapi():
    request = Request('https://btc-e.com/api/2/btc_usd/ticker')
    try:
        response = urlopen(request)
        price = response.read()
        print price[109:116]
    except URLError, e:
        print 'Not Found'

btceapi()

您從API檢索的price變量是

{“ ticker”:{“ high”:298.99899,“ low”:263.20001,“ avg”:281.0995,“ vol”:10566249.17861,“ vol_cur”:37737.87504,“ last”:291,“ buy”:291.493,“賣出“:291.001,”更新“:1436554875,” server_time“:1436554876}}'

那是JSON ,您可以使用以下命令將其解析為字典:

import json

<snip...>
    price = response.read()
    print json.loads(price)["ticker"]["last"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM