×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

What is the quickest way to HTTP GET in Python?

Here, we are going to learn what is the quickest way to HTTP GET in Python programming language?
Submitted by Sapna Deraje Radhakrishna, on March 05, 2020

In order to invoke a HTTP method in python, following libraries can be used:

  1. httplib
  2. urllib
  3. requests

All the above-mentioned libraries can be installed using PIP and the simplest library is the 'requests' library. The example used in this article, are using 'requests', libraries.

Install the library using PIP

pip install requests
Collecting requests
  Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 2.4 MB/s 
Collecting certifi>=2017.4.17
  Downloading certifi-2019.11.28-py2.py3-none-any.whl (156 kB)
     |████████████████████████████████| 156 kB 9.3 MB/s 
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Downloading urllib3-1.25.8-py2.py3-none-any.whl (125 kB)
     |████████████████████████████████| 125 kB 10.6 MB/s 
Collecting chardet<4,>=3.0.2
  Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting idna<3,>=2.5
  Downloading idna-2.9-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 4.1 MB/s 
Installing collected packages: certifi, urllib3, chardet, idna, requests
Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.9 requests-2.23.0 urllib3-1.25.8

Examples of using requests library HTTP GET method

import requests

url = "http://www.recipepuppy.com/api/?i=onions,garlic&q=omelet"

response = requests.get(url=url)

print(response)
print(response.text)

Output

<Response [200]>
{"title":"Recipe Puppy","version":0.1,"href":"http:\/\/www.recipepuppy.com\/","results":[{"title":"Monterey Turkey Omelet","href":"http:\/\/allrecipes.com\/Recipe\/Monterey-Turkey-Omelet\/Detail.aspx","ingredients":"butter, eggs, garlic, green pepper, monterey jack cheese, onions, turkey, water","thumbnail":"http:\/\/img.recipepuppy.com\/5506.jpg"},{"title":"Canadian Bacon Omelet","href":"http:\/\/www.recipezaar.com\/Canadian-Bacon-Omelet-309202","ingredients":"butter, canadian bacon, cheddar cheese, eggs, garlic, onions, potato, red pepper, sour cream","thumbnail":""},{"title":"Cheesy Bacon and Potato Omelet \r\n\r\n","href":"http:\/\/www.kraftfoods.com\/kf\/recipes\/cheesy-bacon-potato-omelet-112465.aspx","ingredients":"bacon, potato, onions, garlic, eggs, cheddar cheese, black pepper, parsley","thumbnail":"http:\/\/img.recipepuppy.com\/600267.jpg"},{"title":"\nShrimp Omelet Recipe\n\n","href":"http:\/\/cookeatshare.com\/recipes\/shrimp-omelet-52483","ingredients":"garlic, onions, vegetable oil, tomato, shrimp, salt, black pepper, eggs","thumbnail":"http:\/\/img.recipepuppy.com\/767245.jpg"},{"title":"Mild Curry Omelet","href":"http:\/\/allrecipes.com\/Recipe\/Mild-Curry-Omelet\/Detail.aspx","ingredients":"coriander,cumin, eggs, garlic, green onion, vegetable oil, onions, red pepper, salt, turmeric","thumbnail":""},{"title":"Greek Omelet","href":"http:\/\/www.recipezaar.com\/Greek-Omelet-311274","ingredients":"capers, eggs, feta cheese, dill weed,garlic, olive oil, olive oil, onions, black pepper, potato, salt, spinach","thumbnail":""},{"title":"Spanish Omelet with Fresh Avocado Salsa","href":"http:\/\/find.myrecipes.com\/recipes\/recipefinder.dyn?action=displayRecipe&recipe_id=366747","ingredients":"sausage, onions, green pepper, garlic, eggs, salt, black pepper, nonstick cooking spray, butter, goat cheese, avocado, black pepper","thumbnail":"http:\/\/img.recipepuppy.com\/550787.jpg"},{"title":"Egyptian Eggplant Omelet","href":"http:\/\/www.recipezaar.com\/egyptian-eggplant-omelet-369516","ingredients":"black pepper, coriander, cumin, eggplant, eggs, garlic, ground beef, onions, parsley, salt","thumbnail":""},{"title":"Zucchini Pepperoni Omelet","href":"http:\/\/www.cooks.com\/rec\/view\/0,1916,138188-236200,00.html","ingredients":"garlic, green pepper, zucchini, pepperoni, onions, olive oil, oregano","thumbnail":""},{"title":"Aussie Omelet","href":"http:\/\/allrecipes.com\/Recipe\/Aussie-Omelet\/Detail.aspx","ingredients":"cheddar cheese, curry powder, eggs, garlic, green pepper, milk,olive oil, onions, salt, shrimp, tomato","thumbnail":""}]}

In the above example, the response received is 200OK and format of the response is JSON. Additionally, some APIs might require request parameters or headers to be appended to the API. In such scenarios, the URL passed to the request method would be:

response = requests.get(url = URL, params = PARAMS)
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.