Using The Requests Library (2024)

What is The requests Library?

Python is “batteries included”, but sometimes the included libraries available in the standard library can be hard to understand. The standard library focuses on functionality, but not necessarily ease of use.

That’s where external libraries come in. The external requests library was developed by Kenneth Reitz to make working with APIs in Python a lot easier. He calls it “HTTP, for humans.” It’s become of the (if not the most) popular Python library!

Our First Request With The requests Library

If you didn’t install the requests library in the working with libraries chapter, you’ll need to do that first before running this code. Use: python -m pip install requests

Let’s make a request to shibe.online to get ourselves some dog pictures. Create a file called shibe.py in our pyworkshop directory and copy this:

# First thing we'll do is import the requests libraryimport requests# Define a variable with the URL of the APIapi_url = "http://shibe.online/api/shibes?count=1"# Call the root of the api with GET, store the answer in a response variable# This call will return a list of URLs that represent dog picturesresponse = requests.get(api_url)# Get the status code for the response. Should be 200 OK# Which means everything worked as expectedprint(f"Response status code is: {response.status_code}")# Get the result as JSONresponse_json = response.json()# Print it. We should see a list with one image URL.print(response_json)
(env) $ python shibe.pyResponse status code is: 200['https://cdn.shibe.online/shibes/28d7c372ea7defdb315ef845285d4ac3906ccea4.jpg']

Dealing with Errors

When dealing with HTTP requests, your first indication of error is usually the HTTP status code. You saw some of the common status codes in the last chapter. The most common status codes are probably 200 - Success, and 404 - Not found. You can find the status code in the status_code property of the response object:

# Passing in a non-existant URL will result in a 404 (not found)bad_response = requests.get("http://shibe.online/api/german-shepards")print(f"Bad Response Status Code is: {bad_response.status_code}") # Status code is 404, meaning that resource doesn’t exist.

Passing in Parameters

# We'll store our base URL here and pass in the count parameter laterapi_url = "http://shibe.online/api/shibes"params = { "count": 10}# Pass those params in with the request.api_response = requests.get(api_url, params=params)print(f"Shibe API Response Status Code is: {api_response.status_code}") # should be 200 OKjson_data = api_response.json()print("Here is a list of URLs for dog pictures:")for url in json_data: print(f"\t {url}")
$ shibe.pyShibe API Response Status Code is: 200Here is a list of URLs for dog pictures: https://cdn.shibe.online/shibes/dfb2af0b2ac1f057750da32f0ea0e154afc160cf.jpg https://cdn.shibe.online/shibes/4989daad2c805ec62b0fb09a80280ba2262f1b08.jpg https://cdn.shibe.online/shibes/a9360b8262c586af2cf53a2d68bb6ec34b87fe25.jpg https://cdn.shibe.online/shibes/a168cc7f2524c73b433afd7c02f698884738daff.jpg https://cdn.shibe.online/shibes/3fbe49908948718c521b756f31dc155ed22941f6.jpg https://cdn.shibe.online/shibes/846bb52389cf9af8a54eb12f48e0e7d0883b17da.jpg https://cdn.shibe.online/shibes/d11ed7f57c5a882f047b921a73f0b95714626bb3.jpg https://cdn.shibe.online/shibes/0fd1dcc9f5866cefaa3040de1be0f8971b0530cd.jpg https://cdn.shibe.online/shibes/cd668ca05d0ec78863f3c30b08b9cd4ff7f5669c.jpg https://cdn.shibe.online/shibes/32bf0797e5a4c5bfb6fc06edc57ddfbf4e08f98f.jpg

More about requests

To learn more about the requests library after class, look at the quick start.

Using The Requests Library (2024)
Top Articles
Hack Analysis: Binance Bridge, October 2022
Have a Lot of Spare Change? Do This With It
Joi Databas
Tmf Saul's Investing Discussions
Walgreens Pharmqcy
Asian Feels Login
How To Be A Reseller: Heather Hooks Is Hooked On Pickin’ - Seeking Connection: Life Is Like A Crossword Puzzle
Black Gelato Strain Allbud
Craigslist Pet Phoenix
Comcast Xfinity Outage in Kipton, Ohio
Over70Dating Login
Hartford Healthcare Employee Tools
Nwi Arrests Lake County
Non Sequitur
Hell's Kitchen Valley Center Photos Menu
How Much Are Tb Tests At Cvs
Parent Resources - Padua Franciscan High School
Las 12 mejores subastas de carros en Los Ángeles, California - Gossip Vehiculos
Marvon McCray Update: Did He Pass Away Or Is He Still Alive?
Keck Healthstream
Tyler Sis University City
Lakewood Campground Golf Cart Rental
All Breed Database
Bjerrum difference plots - Big Chemical Encyclopedia
Stihl Dealer Albuquerque
Wsbtv Fish And Game Report
Suspiciouswetspot
3569 Vineyard Ave NE, Grand Rapids, MI 49525 - MLS 24048144 - Coldwell Banker
Bleacher Report Philadelphia Flyers
Stickley Furniture
Gopher Carts Pensacola Beach
Rugged Gentleman Barber Shop Martinsburg Wv
Ixlggusd
Leland Nc Craigslist
Marine Forecast Sandy Hook To Manasquan Inlet
Goodwill Houston Select Stores Photos
Family Fare Ad Allendale Mi
Skyrim:Elder Knowledge - The Unofficial Elder Scrolls Pages (UESP)
Mckinley rugzak - Mode accessoires kopen? Ruime keuze
1v1.LOL Game [Unblocked] | Play Online
What Is A K 56 Pink Pill?
Pokemon Reborn Gyms
Energy Management and Control System Expert (f/m/d) for Battery Storage Systems | StudySmarter - Talents
The Wait Odotus 2021 Watch Online Free
LoL Lore: Die Story von Caitlyn, dem Sheriff von Piltover
Sara Carter Fox News Photos
Ephesians 4 Niv
Michaelangelo's Monkey Junction
All Obituaries | Roberts Funeral Home | Logan OH funeral home and cremation
Pauline Frommer's Paris 2007 (Pauline Frommer Guides) - SILO.PUB
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6317

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.