0

I've been trying to grab a price from BTC-E price API,I can't just specify price[109:116] for example. because it will just print 2 numbers in the wrong format if such happened. I just need to grab whats after "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()
0

1 Answer 1

3

The price variable you retrieved from the API is

{"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}}'

That's JSON, which you can parse into a dictionary with:

import json

<snip...>
    price = response.read()
    print json.loads(price)["ticker"]["last"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.