在Python中最快的HTTP GET方法是什么?

为了在python中调用HTTP方法,可以使用以下库:

  1. httplib

  2. urllib

  3. requests

可以使用PIP安装所有上述库,最简单的库是“requests库。本文中使用的示例使用的是'requests'库。

使用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

使用请求库HTTP GET方法的示例

import requests

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

response = requests.get(url=url)print(response)print(response.text)

输出结果

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

在上面的示例中,收到的响应为200OK,响应的格式为JSON。此外,某些API可能需要将请求参数或标头附加到API。在这种情况下,传递给request方法的URL将是:

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