പൈത്തൺ സ്ക്രിപ്റ്റ് ഉപയോഗിക്കുന്നത് 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)
ഇവിടെ ഇതുവരെ അഭിപ്രായങ്ങളൊന്നുമില്ല, നിങ്ങൾക്ക് ആദ്യത്തെയാളാകാം!