This class shows you how to use REST API’s in Python. You will get variable values from one API and pass it to another.
NOTE: (ERROR)
requests is not a default module. You may have to use pip3 install requests
- 00:00 Intro
- 06:17 Requests Module to Scrape a Web Page/ REST API
- 16:18 JSON with REST API’s
- 22:21 JSON Module and Prettifying JSON
- 29:40 Demo – Weather Alert Script with Multiple API’s
- 47:46 Final Thoughts
rest-text.py
import requests
ip_address = requests.get('https://api.ipify.org').text
print(ip_address)
requests-copy.py
import requests
page = requests.get('https://arstechnica.com/information-technology/2023/11/sam-altman-officially-back-as-openai-ceo-we-didnt-lose-a-single-employee/').text
print(page)
with open('ars-copy.html', 'w') as file:
file.write(page)
location-json.py
import requests
ip_address = requests.get('https://api.ipify.org').text
location = requests.get(f'http://ip-api.com/json/{ip_address}').json()
print(ip_address)
print(location)
print(f"City:{location['city']} -- State: {location['regionName']}")
pretty-json.py
import requests
import json
ip_address = requests.get('https://api.ipify.org').text
location = requests.get(f'http://ip-api.com/json/{ip_address}').json()
print(location)
location_pretty = json.dumps(location, indent=2)
print(location['city'])
json-module.py
import requests
import json
ip_address = requests.get('https://api.ipify.org').text
#print(ip_address)
location = requests.get(f'http://ip-api.com/json/{ip_address}').json()
state = location['region']
#print(state)
weather = requests.get(f'https://api.weather.gov/alerts/active?area={state}').json()
#weather = requests.get(f'https://api.weather.gov/alerts/active?area=FL').json()
#print(weather)
weather_pretty = json.dumps(weather, indent=2)
#print(weather_pretty)
#print(weather['features'][0]['properties']['headline'])
#print(weather['features'][0]['properties']['description'])
try:
print('WEATHER')
for x in weather['features']:
print('\n --- >>>WARNING!!! <<< ---')
print(x['properties']['headline'])
print(x['properties']['description'])
except:
print('No Alerts')
Be the first to comment