Script Python utilizzando il file requests libreria per recuperare l'attuale tasso di BTC (Bitcoin) da un'API di criptovaluta come CoinGecko:

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)

Questo script invia una richiesta GET all'API CoinGecko, prendendo di mira specificamente il prezzo corrente di Bitcoin rispetto a USD. Quindi analizza la risposta JSON ed estrae il prezzo BTC. Infine, stampa il tasso attuale di BTC.

Prima di eseguire lo script, assicurati di avere il file requests libreria installata (pip install requests). Inoltre, tieni presente i limiti di utilizzo dell'API se effettui richieste frequenti.

Ecco lo script modificato con il supporto proxy 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)

Sostituire 'user:password@proxy_host:proxy_port' con i dettagli del tuo proxy SOCKS5. Se il tuo proxy SOCKS5 non richiede l'autenticazione, puoi omettere il file user:password@ parte.

Commenti (0)

Non ci sono ancora commenti qui, puoi essere il primo!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *


Scegliere e acquistare il Proxy

Proxy per data center

Proxy a rotazione

Proxy UDP

Scelto da oltre 10.000 clienti in tutto il mondo

Cliente proxy
Cliente proxy
Cliente proxy flowch.ai
Cliente proxy
Cliente proxy
Cliente proxy