简体   繁体   English

使用Python获取API

[英]Getting API's using Python

I've been trying to grab a price from BTC-E price API,I can't just specify price[109:116] for example. 我一直在尝试从BTC-E价格API获取价格,例如,我不能仅指定price[109:116] because it will just print 2 numbers in the wrong format if such happened. 因为如果发生这种情况,它只会以错误的格式打印2个数字。 I just need to grab whats after "last:" 我只需要掌握“ 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()

The price variable you retrieved from the API is 您从API检索的price变量是

{"ticker":{"high":298.99899,"low":263.20001,"avg":281.0995,"vol":10566249.17861,"vol_cur":37737.87504,"last":291,"buy":291.493,"sell":291.001,"updated":1436554875,"server_time":1436554876}}' {“ 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}}'

That's JSON , which you can parse into a dictionary with: 那是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