我是靠谱客的博主 苹果眼神,最近开发中收集的这篇文章主要介绍在Python中最快的HTTP GET方法是什么?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

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

  1. httplib

    httplib

  2. urllib

    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.

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

使用PIP安装库 (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

使用请求库HTTP GET方法的示例 (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 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":""}]}

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:

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

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

翻译自: https://www.includehelp.com/python/what-is-the-quickest-way-to-http-get.aspx

最后

以上就是苹果眼神为你收集整理的在Python中最快的HTTP GET方法是什么?的全部内容,希望文章能够帮你解决在Python中最快的HTTP GET方法是什么?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部