Python اسکرپٹ کا استعمال کرتے ہوئے requests
CoinGecko جیسے cryptocurrency API سے موجودہ BTC (Bitcoin) کی شرح حاصل کرنے کے لیے لائبریری:
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 کے مقابلے Bitcoin کی موجودہ قیمت کو نشانہ بناتا ہے۔ یہ پھر JSON جواب کو پارس کرتا ہے اور 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)
یہاں ابھی تک کوئی تبصرہ نہیں ہے، آپ پہلے ہو سکتے ہیں!