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)

このスクリプトは、CoinGecko API に GET リクエストを送信し、特にビットコインの現在の USD に対する価格をターゲットにします。次に、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
代理顧客
代理顧客
代理顧客