如何详细使用Python的urllib3库进行网络请求?
- 内容介绍
- 文章标签
- 相关推荐
本文共计815个文字,预计阅读时间需要4分钟。
`urllib3` 是一个用于 Python 3 的 HTTP 客户端库。Python 标准库提供了 `urllib`,而在 Python 2 中则有 `urllib2`。Python 3 对 `urllib` 和 `urllib2` 进行了重构,将其合并到了标准库中的 `urllib`,并额外提供了 `urllib3`。
1. `urllib3` 的特性: - 线程安全:`urllib3` 是线程安全的,适合在多线程环境中使用。 - 易于使用:提供简洁的 API,方便进行 HTTP 请求。 - 支持多种协议:包括 HTTP/1.1、HTTP/2、HTTPS 等。 - 请求重试:支持自动重试失败的请求。 - 连接池:可以复用连接,提高性能。
urllib3是一款Python 3的HTTP客户端。
Python标准库提供了urllib。在Python 2中,另外提供了urllib2;而在Python 3中,重构了urllib和urllib2到标准库urllib,并另外提供了urllib3。
1. urllib3的特性
线程安全
连接缓冲池
客户端SSL/TLS验证
文件上传
请求重试
HTTP重定向
支持gzip和deflate encoding
支持HTTP和SOCKS的代理
2. 安装
urllib3不是Python 3的标准库,要使用需要另外安装,pip命令如下:
pip install urllib3
3. 用法
1) HTTP GET请求
>>> import urllib3 >>> files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl (57kB) 100% |████████████████████████████████| 61kB 17kB/s Collecting certifi>=2017.4.17 (from requests) Downloading files.pythonhosted.org/packages/18/b0/8146a4f8dd402f60744fa380bc73ca47303cccf8b9190fd16a827281eac2/certifi-2019.9.11-py2.py3-none-any.whl (154kB) 100% |████████████████████████████████| 163kB 18kB/s Collecting idna<2.9,>=2.5 (from requests) Downloading files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB) 100% |████████████████████████████████| 61kB 10kB/s Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests) Downloading files.pythonhosted.org/packages/e0/da/55f51ea951e1b7c63a579c09dd7db825bb730ec1fe9c0180fc77bfb31448/urllib3-1.25.6-py2.py3-none-any.whl (125kB) 100% |████████████████████████████████| 133kB 32kB/s Collecting chardet<3.1.0,>=3.0.2 (from requests) Downloading files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB) 100% |████████████████████████████████| 143kB 48kB/s Installing collected packages: certifi, idna, urllib3, chardet, requests Successfully installed certifi-2019.9.11 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.6 You are using pip version 19.0.3, however version 19.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
3. requests的接口
1) Main interfaces
requests.request() requests.head() requests.get('url', params={'key1':'value1', 'key2':'value2'},headers={'user-agent': '...'}, cookies={'name1':'value2'}) requests.post('url', data={'key':'value'}) requests.post('url', json={'key':'value'}) requests.post('url', files={'uploaded_file': open('report.xls', 'rb')}) requests.post('url', files={'uploaded_file': ('report.xls', open('report.xls', 'rb'), 'application/excel', {'Expires': '0'})}) requests.post('url', files={'uploaded_file': ('temp.txt', 'one line\ntwo lines\n')}) requests.put('url', data={'key':'value'}) requests.patch() requests.delete('url') def getGithub(): github_url = 'api.github.com/user/repos' myresponse = requests.get(github_url, auth=('champagne', 'myPassword')) print(myresponse.json()) def postGithub(): github_url = 'api.github.com/user/repos' data = json.dumps({'name':'python test', 'description':'a python test repo'}) myresponse = requests.post(github_url, data, auth=('champagne', 'myPassword')) print(myresponse.text)
2) requests.Session类
import requests
requests.Session()
3) requests.Request类
import requests
requests.Request('GET', 'api.github.com/events') r.headers['content-type'] #'application/json;charset=utf8' r.url r.status_code #200==requests.codes.ok r.encoding #'utf-8' by default r.raw #raw content r.text #text content r.content #binary content r.json()#json content, recommended r.cookies['a_key']
注意:调用json()方法,如果返回结果不是有效的JSON数据,则抛出ValueError异常。
6) requests.adapters.BaseAdapter类
7) requests.adapters.HTTPAdapter类
requests提供的使用urllib3的HTTP Adapter
以上这篇Python urllib3软件包的使用说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计815个文字,预计阅读时间需要4分钟。
`urllib3` 是一个用于 Python 3 的 HTTP 客户端库。Python 标准库提供了 `urllib`,而在 Python 2 中则有 `urllib2`。Python 3 对 `urllib` 和 `urllib2` 进行了重构,将其合并到了标准库中的 `urllib`,并额外提供了 `urllib3`。
1. `urllib3` 的特性: - 线程安全:`urllib3` 是线程安全的,适合在多线程环境中使用。 - 易于使用:提供简洁的 API,方便进行 HTTP 请求。 - 支持多种协议:包括 HTTP/1.1、HTTP/2、HTTPS 等。 - 请求重试:支持自动重试失败的请求。 - 连接池:可以复用连接,提高性能。
urllib3是一款Python 3的HTTP客户端。
Python标准库提供了urllib。在Python 2中,另外提供了urllib2;而在Python 3中,重构了urllib和urllib2到标准库urllib,并另外提供了urllib3。
1. urllib3的特性
线程安全
连接缓冲池
客户端SSL/TLS验证
文件上传
请求重试
HTTP重定向
支持gzip和deflate encoding
支持HTTP和SOCKS的代理
2. 安装
urllib3不是Python 3的标准库,要使用需要另外安装,pip命令如下:
pip install urllib3
3. 用法
1) HTTP GET请求
>>> import urllib3 >>> files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl (57kB) 100% |████████████████████████████████| 61kB 17kB/s Collecting certifi>=2017.4.17 (from requests) Downloading files.pythonhosted.org/packages/18/b0/8146a4f8dd402f60744fa380bc73ca47303cccf8b9190fd16a827281eac2/certifi-2019.9.11-py2.py3-none-any.whl (154kB) 100% |████████████████████████████████| 163kB 18kB/s Collecting idna<2.9,>=2.5 (from requests) Downloading files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB) 100% |████████████████████████████████| 61kB 10kB/s Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests) Downloading files.pythonhosted.org/packages/e0/da/55f51ea951e1b7c63a579c09dd7db825bb730ec1fe9c0180fc77bfb31448/urllib3-1.25.6-py2.py3-none-any.whl (125kB) 100% |████████████████████████████████| 133kB 32kB/s Collecting chardet<3.1.0,>=3.0.2 (from requests) Downloading files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB) 100% |████████████████████████████████| 143kB 48kB/s Installing collected packages: certifi, idna, urllib3, chardet, requests Successfully installed certifi-2019.9.11 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.6 You are using pip version 19.0.3, however version 19.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
3. requests的接口
1) Main interfaces
requests.request() requests.head() requests.get('url', params={'key1':'value1', 'key2':'value2'},headers={'user-agent': '...'}, cookies={'name1':'value2'}) requests.post('url', data={'key':'value'}) requests.post('url', json={'key':'value'}) requests.post('url', files={'uploaded_file': open('report.xls', 'rb')}) requests.post('url', files={'uploaded_file': ('report.xls', open('report.xls', 'rb'), 'application/excel', {'Expires': '0'})}) requests.post('url', files={'uploaded_file': ('temp.txt', 'one line\ntwo lines\n')}) requests.put('url', data={'key':'value'}) requests.patch() requests.delete('url') def getGithub(): github_url = 'api.github.com/user/repos' myresponse = requests.get(github_url, auth=('champagne', 'myPassword')) print(myresponse.json()) def postGithub(): github_url = 'api.github.com/user/repos' data = json.dumps({'name':'python test', 'description':'a python test repo'}) myresponse = requests.post(github_url, data, auth=('champagne', 'myPassword')) print(myresponse.text)
2) requests.Session类
import requests
requests.Session()
3) requests.Request类
import requests
requests.Request('GET', 'api.github.com/events') r.headers['content-type'] #'application/json;charset=utf8' r.url r.status_code #200==requests.codes.ok r.encoding #'utf-8' by default r.raw #raw content r.text #text content r.content #binary content r.json()#json content, recommended r.cookies['a_key']
注意:调用json()方法,如果返回结果不是有效的JSON数据,则抛出ValueError异常。
6) requests.adapters.BaseAdapter类
7) requests.adapters.HTTPAdapter类
requests提供的使用urllib3的HTTP Adapter
以上这篇Python urllib3软件包的使用说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

