python get 请求_Python请求入门-GET请求

python get 请求

In many web apps, it's normal to connect to various third-party services by using APIs. When you use these APIs you can get access to data like weather information, sports scores, movie listings, tweets, search engine results, and pictures. You can also use APIs to add functionality to your app. Examples of these are payments, scheduling, emails, translations, maps, and file transfers. If you were to create any of those on your own it would take a ton of time, but with APIs, it can take only minutes to connect to one and access its features and data.

在许多Web应用程序中,通常使用API​​连接到各种第三方服务。 使用这些API时,您可以访问天气信息,体育比分,电影列表,推文,搜索引擎结果和图片等数据。 您还可以使用API​​向您的应用添加功能。 这些示例包括付款,计划,电子邮件,翻译,地图和文件传输。 如果您要自己创建任何一个,则将花费大量时间,但是使用API​​,只需几分钟即可连接到一个并访问其功能和数据。

In this article, I'm going to talk about the Python Requests library, which allows you to send HTTP requests in Python.

在本文中,我将讨论Python请求库,该库允许您使用Python发送HTTP请求。

And since using an API is simply sending HTTP requests and receiving responses, Requests allows you to use APIs in Python. I'll demonstrate the use of a language translation API here so you can see an example of how it works so you can potentially use it in your own apps.

并且由于使用API​​只是发送HTTP请求和接收响应,因此Requests允许您在Py​​thon中使用API​​。 我将在此处演示语言翻译API的用法,以便您看到其工作方式的示例,以便有可能在自己的应用中使用它。

HTTP请求快速概述 ( Quick Overview of HTTP Requests )

HTTP requests are how the web works. Every time you navigate to a web page, your browser makes multiple requests to the web page's server. The server then responds with all the data necessary to render the page, and your browser then actually renders the page so you can see it.

HTTP请求是网络的工作方式。 每次导航到网页时,浏览器都会向该网页的服务器发出多个请求。 然后,服务器将提供呈现页面所需的所有数据进行响应,然后您的浏览器将实际呈现该页面,以便您可以看到它。

The generic process is this: a client (like a browser or Python script using Requests) will send some data to a URL, and then the server located at the URL will read the data, decide what to do with it, and return a response to the client. Finally, the client can decide what to do with the data in the response.

通用过程是这样的:客户端(例如使用Requests的浏览器或Python脚本)将向URL发送一些数据,然后位于URL的服务器将读取数据,决定如何处理并返回响应给客户。 最后,客户端可以决定如何处理响应中的数据。

Part of the data the client sends in a request is the request method. Some common request methods are GET, POST, and PUT. GET requests are normally for reading data only without making a change to something, while POST and PUT requests generally are for modifying data on the server. So for example, the Stripe API allows you to use POST requests to create a new charge so a user can purchase something from your app.

客户端在请求中发送的部分数据是请求方法。 一些常见的请求方法是GET,POST和PUT。 GET请求通常仅用于读取数据而不进行任何更改,而POST和PUT请求通常用于修改服务器上的数据。 因此,例如,Stripe API允许您使用POST请求创建新的费用,以便用户可以从您的应用程序中购买商品。

This article will cover GET requests only because we won't be modifying any data on a server.

本文仅介绍GET请求,因为我们不会在服务器上修改任何数据。

When sending a request from a Python script or inside a web app, you, the developer, gets to decide what gets sent in each request and what to do with the response. So let's explore that by first sending a request to Scotch.io and then by using a language translation API.

从Python脚本或网络应用程序发送请求时,开发人员可以决定在每个请求中发送了什么以及如何处理响应。 因此,让我们通过首先向Scotch.io发送请求,然后使用语言翻译API来进行探索。

安装Python请求 ( Install Python Requests )

Before we can do anything, we need to install the library. So let's go ahead and install requests using pip. It's a good idea to create a virtual environment first if you don't already have one.

在做任何事情之前,我们需要安装该库。 因此,让我们继续使用pip安装请求。 如果您还没有虚拟环境,那么最好先创建一个虚拟环境。

pipinstall requests

我们的第一个要求 ( Our First Request )

To start, let's use Requests for something simple: requesting the Scotch.io site. Create a file called script.py and add the following code to it. In this article, we won't have much code to work with, so when something changes you can just update the existing code instead of adding new lines.

首先,让我们使用Requests做一些简单的事情:请求Scotch.io网站。 创建一个名为script.py的文件,并向其中添加以下代码。 在本文中,我们没有太多代码可以使用,因此,当发生更改时,您可以更新现有代码,而不必添加新行。

im

你可能感兴趣的:(python,java,linux,编程语言,ajax)