Created
October 24, 2019 15:56
-
-
Save swang373/0887b2b7399e0e3a42a949dc988d8341 to your computer and use it in GitHub Desktop.
Example of URL fetching function using requests library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
class HTTPRequestError(Exception): | |
pass | |
def fetch(url, params=None, **kwargs): | |
try: | |
response = requests.get(url, params, **kwargs) | |
if response.status_code == 200: | |
return response | |
else: | |
raise HTTPRequestError(f'The GET request to {url} responded with status code {response.status_code}') | |
except Exception: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment