Sometimes we gather data form API's. JSON is the most common formats we receiving output.
Here I am showing how to call api's and handle errors.
step1: Import required packages
- import requests
- import json
step2: Using requests call api url. here "base_url" is web service url.
We can make a GET request to this API endpoint with requests.get
- base_url = "https://api.exchangeratesapi.io/latest"
- response = requests.get(base_url)
This method returns the response from the server
We store this response in a variable for future processing
step3: Verify if the request went through ok or not
- response.ok (It returns True if success)
- response.status_code (for my case it returned 200). we can check status code meaning
- Incase of error 400 code will be return
- response.json() will be show error details
step4: Verify data
- response.text ( returns output in string format)
- response.content (returns the content of the response in byte format)
Uploaded work to github. Please find the link below.
github: https://github.com/kvsivasankar/web-scraping/blob/master/webscraping1.ipynb
No comments:
Post a Comment