Tập lệnh Python sử dụng requests thư viện để lấy tỷ lệ BTC (Bitcoin) hiện tại từ API tiền điện tử như 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)

Tập lệnh này gửi yêu cầu GET tới API CoinGecko, nhắm mục tiêu cụ thể đến giá hiện tại của Bitcoin so với USD. Sau đó, nó phân tích phản hồi JSON và trích xuất giá BTC. Cuối cùng, nó in tỷ giá BTC hiện tại.

Trước khi chạy tập lệnh, hãy đảm bảo bạn có requests thư viện đã được cài đặt (pip install requests). Ngoài ra, hãy lưu ý đến giới hạn sử dụng API nếu bạn thường xuyên đưa ra các yêu cầu.

Đây là tập lệnh được sửa đổi có hỗ trợ 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)

Thay thế 'user:password@proxy_host:proxy_port' với chi tiết proxy SOCKS5 của bạn. Nếu proxy SOCKS5 của bạn không yêu cầu xác thực, bạn có thể bỏ qua user:password@ phần.

Bình luận (0)

Chưa có bình luận nào ở đây, bạn có thể là người đầu tiên!

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *


Chọn và mua proxy

Proxy trung tâm dữ liệu

Proxy luân phiên

Proxy UDP

Được tin cậy bởi hơn 10000 khách hàng trên toàn thế giới

Khách hàng ủy quyền
Khách hàng ủy quyền
Khách hàng proxy flowch.ai
Khách hàng ủy quyền
Khách hàng ủy quyền
Khách hàng ủy quyền