Python betiğini kullanarak requests Mevcut BTC (Bitcoin) oranını CoinGecko gibi bir kripto para birimi API'sinden almak için kütüphane:

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)

Bu komut dosyası, CoinGecko API'sine, özellikle Bitcoin'in USD karşısındaki mevcut fiyatını hedefleyen bir GET isteği gönderir. Daha sonra JSON yanıtını ayrıştırır ve BTC fiyatını çıkarır. Son olarak mevcut BTC kurunu yazdırır.

Komut dosyasını çalıştırmadan önce, sahip olduğunuzdan emin olun. requests kütüphane kuruldu (pip install requests). Ayrıca sık istekte bulunuyorsanız API kullanım sınırlarına dikkat edin.

İşte SOCKS5 proxy desteğine sahip değiştirilmiş komut dosyası:

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)

Değiştirin 'user:password@proxy_host:proxy_port' SOCKS5 proxy ayrıntılarınızla. SOCKS5 proxy'niz kimlik doğrulama gerektirmiyorsa, user:password@ parça.

Yorumlar (0)

Burada henüz yorum yok, ilk siz olabilirsiniz!

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir


Proxy Seçin ve Satın Alın

Veri Merkezi Proxyleri

Dönen Proxyler

UDP Proxyleri

Dünya Çapında 10.000'den Fazla Müşterinin Güvendiği

Vekil Müşteri
Vekil Müşteri
Vekil Müşteri flowch.ai
Vekil Müşteri
Vekil Müşteri
Vekil Müşteri