Python 스크립트를 사용하여 requests CoinGecko와 같은 암호화폐 API에서 현재 BTC(비트코인) 환율을 가져오는 라이브러리:

import requests

def get_btc_rate():
    try:
        # URL for CoinGecko API to fetch BTC price
        url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd'

        # Sending GET request to the API
        response = requests.get(url)

        # Checking if request was successful (status code 200)
        if response.status_code == 200:
            # Parsing JSON response
            data = response.json()

            # Extracting BTC price from response
            btc_price = data['bitcoin']['usd']

            return btc_price
        else:
            # If request fails, print error message
            print("Failed to fetch BTC rate. Status code:", response.status_code)
            return None
    except Exception as e:
        # Print any exceptions that occur during the process
        print("An error occurred:", str(e))
        return None

# Example usage:
btc_rate = get_btc_rate()
if btc_rate is not None:
    print("Current BTC rate: $", btc_rate)

이 스크립트는 특히 USD 대비 비트코인의 현재 가격을 대상으로 CoinGecko API에 GET 요청을 보냅니다. 그런 다음 JSON 응답을 구문 분석하고 BTC 가격을 추출합니다. 마지막으로 현재 BTC 비율을 인쇄합니다.

스크립트를 실행하기 전에 requests 라이브러리 설치됨(pip install requests). 또한 자주 요청하는 경우 API 사용 제한에 유의하세요.

SOCKS5 프록시를 지원하는 수정된 스크립트는 다음과 같습니다.

import requests

def get_btc_rate(proxy=None):
    try:
        # URL for CoinGecko API to fetch BTC price
        url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd'

        # Defining proxy settings if provided
        proxies = {'http': proxy, 'https': proxy} if proxy else None

        # Sending GET request to the API with proxy settings
        response = requests.get(url, proxies=proxies)

        # Checking if request was successful (status code 200)
        if response.status_code == 200:
            # Parsing JSON response
            data = response.json()

            # Extracting BTC price from response
            btc_price = data['bitcoin']['usd']

            return btc_price
        else:
            # If request fails, print error message
            print("Failed to fetch BTC rate. Status code:", response.status_code)
            return None
    except Exception as e:
        # Print any exceptions that occur during the process
        print("An error occurred:", str(e))
        return None

# Example usage with SOCKS5 proxy
proxy_address = 'socks5://user:password@proxy_host:proxy_port'  # Replace with your SOCKS5 proxy details
btc_rate = get_btc_rate(proxy=proxy_address)
if btc_rate is not None:
    print("Current BTC rate: $", btc_rate)

교체 'user:password@proxy_host:proxy_port' SOCKS5 프록시 세부정보로 SOCKS5 프록시에 인증이 필요하지 않은 경우 다음을 생략할 수 있습니다. user:password@ 부분.

댓글 (0)

여기에는 아직 댓글이 없습니다. 첫 번째 댓글이 되실 수 있습니다!

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다


프록시 선택 및 구매

데이터센터 프록시

회전 프록시

UDP 프록시

전 세계 10,000명 이상의 고객이 신뢰함

대리 고객
대리 고객
대리 고객 flowch.ai
대리 고객
대리 고객
대리 고객