पायथन स्क्रिप्ट का उपयोग कर requests
लाइब्रेरी का उपयोग करके कॉइनगेको जैसी क्रिप्टोकरेंसी एपीआई से वर्तमान बीटीसी (बिटकॉइन) दर प्राप्त करें:
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)
यहां अभी तक कोई टिप्पणी नहीं है, आप पहले हो सकते हैं!