首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Coingecko Python包装器与代理一起使用?

如何将Coingecko Python包装器与代理一起使用?
EN

Stack Overflow用户
提问于 2022-01-20 08:51:22
回答 1查看 106关注 0票数 0

CoinGecko服务有一个Python包装器,可以像这样连接:

代码语言:javascript
复制
from pycoingecko import CoinGeckoAPI
cg = CoinGeckoAPI()
price = cg.get_coin_by_id('tether')

我有一个python代码,它解析а空闲代理站点并返回随机代理dict,如{'https':'187.62.191.3:61256'}

代码语言:javascript
复制
import requests
from bs4 import BeautifulSoup
from random import choice

def get_proxy():
    url = "https://www.ssl-site.com/"
    r = requests.get(url)
    soup = BeautifulSoup(r.content, 'html5lib')
    return{'https': choice(list(map(lambda x:x[0]+':'+x[1], list(zip(map(lambda x:x.text, soup.findAll('td')[::8]), map(lambda x:x.text, soup.findAll('td')[1::8]))))))}

get_proxy()



def proxy_request(request_type, url, **kwargs):
    while 1:
        try:
            proxy = get_proxy()
            print('Using proxy: {}'.format(proxy))
            r = requests.request(request_type, url, proxies=proxy, timeout=5, **kwargs)
            break
        except:
            pass
    return r

如何每次从新ip调用CoinGecko API包装终结点?

EN

回答 1

Stack Overflow用户

发布于 2022-11-04 13:20:37

您可以在直接使用session属性初始化CoinGeckoAPI()之后设置代理,然后调用任何API端点。例如:

代码语言:javascript
复制
import pycoingecko
cg=pycoingecko.CoinGeckoAPI()

# set up a proxy (or any other dictionary with proxies)
cg.session.proxies['https']='http://10.10.1.10:1080'

# you can check proxies by printing them
print(cg.session.proxies)
# output here should be: {'https': 'http://10.10.1.10:1080'}

# call an endpoint, for example get price of bitcoin in USD
cg.get_price(ids='bitcoin', vs_currencies='usd')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70783198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档