I'm requesting list of coins
requests.get(
"https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&order=market_cap_desc&per_page=4&page=1&sparkline=false").text
Problem is that there is a parameter called "per_page" with a limit. So in order to get all coins with one request I need to increment the "page" parameter by 1. Also there is no telling of how many pages there are. Is there a fix for this kinda situation? Maybe a loop that incerements the value ?
CodePudding user response:
Looking at the URL it seems there's max 250 coins, so icrease the per_page=
parameter:
import requests
url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&order=market_cap_desc&per_page=999&page=1&sparkline=false"
data = requests.get(url).json()
for i, d in enumerate(data, 1):
print("{:>3}. {}".format(i, d["name"]))
Prints:
...
237. Kadena
238. Ark
239. AllianceBlock
240. Adventure Gold
241. Sapphire
242. cUNI
243. Balancer
244. FLEX Coin
245. Casper Network
246. DerivaDAO
247. Unizen
248. IDEX
249. Enzyme
250. sBTC
EDIT: To load multiple pages:
import json
import requests
url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&order=market_cap_desc&per_page=4&page={}&sparkline=false"
cnt, i = 1, 1
while True:
try:
data = requests.get(url.format(i)).text
data = json.loads(data)
except:
print("Error!")
break
if not data:
break
for d in data:
print("{:>3}. {}".format(cnt, d["name"]))
cnt = 1
i = 1
CodePudding user response:
If there was a maximum number of coins to request and the API would allow you to surpass it, then that would be the solution. But then you would not be here, so it's safe to assume that the situation is worse.
Let's take a seemingly large number, maybe 1000 (or the value you choose). Perform the following algorithm:
offset <- 0
limit <- 1000
do
items <- request(offset, offset limit)
offset <- offset limit
doSomethingWith(items)
while not items.empty
processResults()