¿Cuál es la forma más rápida de HTTP GET en Python?

Aquí, vamos a aprender ¿cuál es la forma más rápida de HTTP GET en el lenguaje de programación Python?

Con el fin de invocar un método HTTP en pitón, siguientes bibliotecas se pueden utilizar:

  1. httplib
  2. urllib
  3. solicitudes

Todas las bibliotecas antes mencionadas pueden ser instalados usando PIP y la biblioteca más simple es el “ las peticiones de los biblioteca. El ejemplo utilizado en este artículo, están utilizando ‘peticiones’ , bibliotecas.

Instalar la biblioteca utilizando 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

Ejemplos del uso de solicitudes biblioteca HTTP GET método

import requests
url = "http://www.recipepuppy.com/api/?i=onions,garlic&q=omelet"
response = requests.get(url=url)
print(response)
print(response.text)

salida

<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 rnrn","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 Recipenn","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":""}]}

En el ejemplo anterior, la respuesta recibida es 200OK y el formato del la respuesta es JSON. Además, algunas APIs pueden requerir parámetros de la petición o cabeceras que se anexan a la API. En estos escenarios, la URL se pasa al método solicitud sería:

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


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *