python学习之 requests库

本文介绍如何使用Python的Requests库简化HTTP请求操作,包括GET、POST等请求方式,并讲解了如何处理请求参数、响应内容、自定义头部信息及管理Cookies。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方文档http://docs.python-requests.org/en/master/user/quickstart/#make-a-request

requests是urllib的一个封装,能够用简单的html方法来实现我们需要的操作,同时在cookie,headers等方面都能够实现自我定制

Make a Request

Making a request with Requests is very simple.

Begin by importing the Requests module:

>>> import requests

Now, let's try to get a webpage. For this example, let's get GitHub's public timeline:

>>> r = requests.get('https://api.github.com/events')

Now, we have a Response object called r. We can get all the information we need from this object.

Requests' simple API means that all forms of HTTP request are as obvious. For example, this is how you make an HTTP POST request:

>>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})

Nice, right? What about the other HTTP request types: PUT, DELETE, HEAD and OPTIONS? These are all just as simple:

>>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})
>>> r = requests.delete('http://httpbin.org/delete')
>>> r = requests.head('http://httpbin.org/get')
>>> r = requests.options('http://httpbin.org/get')

That's all well and good, but it's also only the start of what Requests can do.

Passing Parameters In URLs

You often want to send some sort of data in the URL's query string. If you were constructing the URL by hand, this data would be given as key/value pairs in the URL after a question mark, e.g.httpbin.org/get?key=val. Requests allows you to provide these arguments as a dictionary, using theparams keyword argument. As an example, if you wanted to pass key1=value1 and key2=value2 tohttpbin.org/get, you would use the following code:

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get('http://httpbin.org/get', params=payload)

You can see that the URL has been correctly encoded by printing the URL:

>>> print(r.url)
http://httpbin.org/get?key2=value2&key1=value1

Note that any dictionary key whose value is None will not be added to the URL's query string.

You can also pass a list of items as a value:

>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}

>>> r = requests.get('http://httpbin.org/get', params=payload)
>>> print(r.url)
http://httpbin.org/get?key1=value1&key2=value2&key2=value3

Response Content

We can read the content of the server's response. Consider the GitHub timeline again:

>>> import requests

>>> r = requests.get('https://api.github.com/events')
>>> r.text
u'[{"repository":{"open_issues":0,"url":"https://github.com/...

Requests will automatically decode content from the server. Most unicode charsets are seamlessly decoded.

When you make a request, Requests makes educated guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by Requests is used when you access r.text. You can find out what encoding Requests is using, and change it, using the r.encoding property:

>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1'

If you change the encoding, Requests will use the new value of r.encoding whenever you call r.text. You might want to do this in any situation where you can apply special logic to work out what the encoding of the content will be. For example, HTTP and XML have the ability to specify their encoding in their body. In situations like this, you should use r.content to find the encoding, and then set r.encoding. This will let you use r.text with the correct encoding.

Requests will also use custom encodings in the event that you need them. If you have created your own encoding and registered it with the codecs module, you can simply use the codec name as the value ofr.encoding and Requests will handle the decoding for you.


JSON Response Content

There's also a builtin JSON decoder, in case you're dealing with JSON data:

>>> import requests

>>> r = requests.get('https://api.github.com/events')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...

In case the JSON decoding fails, r.json() raises an exception. For example, if the response gets a 204 (No Content), or if the response contains invalid JSON, attempting r.json() raises ValueError: No JSONobject could be decoded.

It should be noted that the success of the call to r.json() does not indicate the success of the response. Some servers may return a JSON object in a failed response (e.g. error details with HTTP 500). Such JSON will be decoded and returned. To check that a request is successful, use r.raise_for_status() or checkr.status_code is what you expect.

Raw Response Content

In the rare case that you'd like to get the raw socket response from the server, you can access r.raw. If you want to do this, make sure you set stream=True in your initial request. Once you do, you can do this:

>>> r = requests.get('https://api.github.com/events', stream=True)

>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>

>>> r.raw.read(10)

Custom Headers

If you'd like to add HTTP headers to a request, simply pass in a dict to the headers parameter.

For example, we didn't specify our user-agent in the previous example:

>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}

>>> r = requests.get(url, headers=headers)

Note: Custom headers are given less precedence than more specific sources of information. For instance:

  • Authorization headers set with headers= will be overridden if credentials are specified in .netrc, which in turn will be overridden by the auth= parameter.
  • Authorization headers will be removed if you get redirected off-host.
  • Proxy-Authorization headers will be overridden by proxy credentials provided in the URL.
  • Content-Length headers will be overridden when we can determine the length of the content.

Furthermore, Requests does not change its behavior at all based on which custom headers are specified. The headers are simply passed on into the final request.

Note: All header values must be a string, bytestring, or unicode. While permitted, it's advised to avoid passing unicode header values.


Cookies

If a response contains some Cookies, you can quickly access them:

>>> url = 'http://example.com/some/cookie/setting/url'
>>> r = requests.get(url)

>>> r.cookies['example_cookie_name']
'example_cookie_value'

To send your own cookies to the server, you can use the cookies parameter:

>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')

>>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'

Cookies are returned in a RequestsCookieJar, which acts like a dict but also offers a more complete interface, suitable for use over multiple domains or paths. Cookie jars can also be passed in to requests:

>>> jar = requests.cookies.RequestsCookieJar()
>>> jar.set('tasty_cookie', 'yum', site='httpbin.org', path='/cookies')
>>> jar.set('gross_cookie', 'blech', site='httpbin.org', path='/elsewhere')
>>> url = 'http://httpbin.org/cookies'
>>> r = requests.get(url, cookies=jar)
>>> r.text
'{"cookies": {"tasty_cookie": "yum"}}'


<think>好的,我现在需要帮助用户学习如何使用Pythonrequests进行网络请求。首先,用户明确提到想要一个使用教程和示例,所以我应该从基础开始,逐步介绍常用的HTTP方法,比如GET和POST,以及错误处理和高级功能,比如设置请求头和代理。 首先,用户的问题是关于requests的使用,所以我需要确保覆盖主要的请求类型。根据引用[4],GET请求可以通过params参数传递URL参数,这应该作为第一个示例。然后,POST请求通常需要传递数据,比如表单或JSON,这也是常见的用例。 接下来,错误处理部分,引用[1]提到了Requests可以抛出异常,比如HTTPError和Timeout,所以需要在教程中加入try-except块来处理这些异常,确保用户知道如何捕获和处理常见的网络问题。 另外,用户提供的引用[3]提到了代理请求,这部分可以作为高级用法来介绍。安装requests之后,设置代理的方法需要详细说明,比如在get方法中传递proxies参数。 然后,请求头的设置也很重要,引用[4]中提到了在GET请求中设置请求头的例子,所以这里应该包括如何自定义User-Agent或其他头部信息。 关于安装部分,用户可能已经知道,但为了完整性,还是应该简要提及,如引用[3]所示,使用pip安装requests。 在组织内容时,需要按照逻辑顺序,从安装到基本请求,再到高级功能,最后是错误处理。每个部分都应有代码示例,并保持代码简洁,符合用户提供的示例中的格式,比如使用代码块,并确保语法正确。 同时,用户要求生成相关问题,可能需要包括性能优化、会话对象的使用、文件上传、证书验证等,这些问题可以帮助用户进一步探索。 最后,检查所有引用是否正确对应,比如引用[1]关于异常处理,引用[3]关于代理,引用[4]关于GET参数和请求头。确保引用标记正确,没有遗漏或错误。 现在需要确保回答结构清晰,分步骤讲解,每个部分都有示例代码,并且自然融入引用。同时,问题部分需要涵盖用户可能感兴趣的后续主题,比如会话管理、文件上传、身份验证等。</think>### Python Requests使用教程 #### 一、安装与基础使用 1. **安装** 使用pip安装(如引用[3]所述): ```bash pip install requests ``` 2. **发送GET请求** 通过`params`传递URL参数(如引用[4]示例): ```python import requests params = {'q': 'requests+language:python'} response = requests.get('https://api.github.com/search/repositories', params=params) print(response.status_code) # 输出状态码 print(response.json()) # 解析JSON响应 ``` 3. **自定义请求头** 添加`headers`参数模拟浏览器访问: ```python headers = {'User-Agent': 'Mozilla/5.0'} response = requests.get('https://example.com', headers=headers) ``` --- #### 二、POST请求与数据提交 1. **提交表单数据** 使用`data`参数发送表单: ```python data = {'username': 'admin', 'password': 'test'} response = requests.post('https://httpbin.org/post', data=data) ``` 2. **发送JSON数据** 使用`json`参数自动序列化: ```python json_data = {'key': 'value'} response = requests.post('https://api.example.com/submit', json=json_data) ``` --- #### 三、错误处理(如引用[1]示例) ```python from requests.exceptions import HTTPError, Timeout try: response = requests.get('https://api.github.com', timeout=1) response.raise_for_status() # 检查HTTP错误 except HTTPError as http_err: print(f'HTTP错误: {http_err}') except Timeout as timeout_err: print(f'超时错误: {timeout_err}') except Exception as err: print(f'其他错误: {err}') ``` --- #### 四、高级功能 1. **代理设置**(如引用[3]所述) 通过`proxies`参数配置代理: ```python proxies = {'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080'} response = requests.get('https://example.com', proxies=proxies) ``` 2. **会话保持** 使用`Session`对象复用TCP连接: ```python with requests.Session() as s: s.get('https://example.com/login') # 保持Cookies response = s.get('https://example.com/dashboard') ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值