Title
Examples of APIs
# Begin Rapid API Code
import requests
url = "https://coinranking1.p.rapidapi.com/coins"
querystring = {"referenceCurrencyUuid":"yhjMzLPhuIDl","timePeriod":"24h","tiers[0]":"1","orderBy":"marketCap","orderDirection":"desc","limit":"50","offset":"0"}
headers = {
"X-RapidAPI-Key": "2348020badmshecfd384514830c4p1f8a42jsn72e4bf449cdf", # place your key here
"X-RapidAPI-Host": "coinranking1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
#print(response.text),code that was here, and prints data but not organized
# End Rapid API Code
json = response.json() # convert response to python json object
# Observe data from an API. This is how data transports over the internet in a "JSON" text form
# - The JSON "text" is formed in dictionary {} and list [] divisions
# - To read the result, Data Scientist of Developer converts JSON into human readable form
# - Review the first line, look for the keys -- "status" and "data"
for coin in json["data"]["coins"]:
print(f'{coin["symbol"]} {coin["name"]} {coin["price"]}')
"""
Requests is a HTTP library for the Python programming language.
The goal of the project is to make HTTP requests simpler and more human-friendly.
"""
import requests
url = "https://corona-virus-world-and-india-data.p.rapidapi.com/api"
headers = {
"X-RapidAPI-Key": "561be3d3dcmsh26da5c03e252f9fp1d2d4fjsn25f9e92f38d8",
"X-RapidAPI-Host": "corona-virus-world-and-india-data.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
print(response.json)
# This code looks for "world data"
print("World Totals")
world = response.json().get('world_total') # turn response to json() so we can extract "world_total"
for key, value in world.items(): # this finds key, value pairs in country
print(key, value)
print()
# This code looks for USA in "countries_stats"
print("Country Totals")
countries = response.json().get('countries_stat')
for country in countries: # countries is a list
if country["country_name"] == "USA": # this filters for USA
for key, value in country.items(): # this finds key, value pairs in country
print(key, value)