2

I am using the following code to look up json from the poloniex API. It is returning HTML instead of JSON. What am I doing wrong? I would like JSON.

import requests
res = requests.get("https://poloniex.com/public?command=returnTradeHistory&currencyPair=BTC_NXT&start=1410158341&end=1410499372")
print(res.text)
Thomas Fauskanger
  • 2,536
  • 1
  • 27
  • 42
Jonathan Math
  • 101
  • 2
  • 7
  • 3
    Cannot reproduce; I'm getting JSON using both Python 3.6.3 with requests 2.18.4 and Python 2.7.10 with requests 2.11.1. – jwodder Dec 24 '17 at 01:52
  • "There are six public methods, all of which take HTTP GET requests and return output in JSON format" [docs](https://poloniex.com/support/api/) . What does your response look like? – Thomas Fauskanger Dec 24 '17 at 01:53
  • It looks like this Poloniex - Bitcoin/Cryptocurrency Exchange – Jonathan Math Dec 24 '17 at 01:56
  • What is on your screen if you visit in [browser?](https://poloniex.com/public?command=returnTradeHistory&currencyPair=BTC_NXT&start=1410158341&end=1410499372) – Thomas Fauskanger Dec 24 '17 at 01:57
  • 1
    In my case ``res.json()`` gives a list of 1112 json elements. Python3.6.3, latest requests. – Thomas Fauskanger Dec 24 '17 at 01:59
  • it has a captcha security check before giving me the json. – Jonathan Math Dec 24 '17 at 02:00
  • 2
    I see, perhaps you have exceeded some limit of API? – Thomas Fauskanger Dec 24 '17 at 02:01
  • I get "raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" when I use res.json() – Jonathan Math Dec 24 '17 at 02:02
  • There's no way I've exceeded the API limit – Jonathan Math Dec 24 '17 at 02:02
  • 1
    Yes, because in your case it's HTML as you said. I read in their API docs, linked over, that "Please note that making more than 6 calls per second to the public API, or repeatedly and needlessly fetching excessive amounts of data, can result in your IP being banned." – Thomas Fauskanger Dec 24 '17 at 02:02
  • It's odd indeed. – Thomas Fauskanger Dec 24 '17 at 02:03
  • I've only done like one request per minute. – Jonathan Math Dec 24 '17 at 02:05
  • 1
    I did not have to pass the Captcha, I got straight to the json in my browser too. – Thomas Fauskanger Dec 24 '17 at 02:05
  • It didn't work even after you passed the captcha? Or do you have to pass it on refresh? – Thomas Fauskanger Dec 24 '17 at 02:06
  • [Here is a tweet about it](https://twitter.com/poloniex/status/455474093606055936) from 2014, and it seems [others have/had](https://github.com/s4w3d0ff/python-poloniex/issues/35) the same problem. (They didn't mention solution) – Thomas Fauskanger Dec 24 '17 at 02:10
  • There seems to be an alpha release of a [poloniex](https://pypi.python.org/pypi/poloniex) python package on PyPI, perhaps it will help you get around the issue. – Thomas Fauskanger Dec 24 '17 at 02:14
  • 2
    I'm voting to close this question as off-topic because it hinges on the access policies of a specific vendor's API and is thus out of SO scope. – msw Dec 24 '17 at 03:19

2 Answers2

2

To summarize the comment chain, you are almost certainly doing something that the server doesn't like which causes it to ask you for a CAPTCHA.

One of the commenters pointed out the documented limit of "6 calls per second to the public API, or repeatedly and needlessly fetching excessive amounts of data". We can only guess at what "repeatedly" or "needlessly" mean to the service, but I think you've made it at least suspicious because it is asking (via CAPTCHA) "are you a person or a program?".

If you want to know what the service's actual limits are, contact the firm. You might have to pay to get the data you are looking for.

msw
  • 42,753
  • 9
  • 87
  • 112
  • Captcha are also country dependent. If I use a USA proxy and access the API, it works as expected, but for example from India it returns a CATPCHA. – Mehul Jan 08 '18 at 09:53
0

Thanks to a comment of user Mehul, I solved the same problem, but using PHP. I was trying for 2 hours to make a simple curl() to retrieve the public api using this simple call:

https://poloniex.com/public?command=returnTicker

The problem for me was that I was trying to do the call from my local server (at home). So thanks to Mehul, I decided to test to upload the script to my server and voilà: it immediately ran as expected, returning the json array.

My server is in the USA, although I'm in Mexico. Maybe there will be a problem of IPs and geolocalizations. Or a problem "home" vs. "server" connection. I don't know, but I hope that my approach was useful to other people. I really had problems to find some help with this issue. Ep, and I've implemented some API integrations with other exchanges!

Mr. T
  • 11,960
  • 10
  • 32
  • 54
caos30
  • 31
  • 7