文章概览:
1. 简单介绍网络请求常用的python库
2. 对比Requests和urllib的异同
3. 结合代码示例说明两个库的差异
1. 简介
Python中处理网络请求的相关库有很多,其中一些常用的包括:
-
requests:一个简洁且功能强大的HTTP库,用于发送HTTP请求和处理响应。它是Python中最常用的网络请求库之一,使用简单,易于上手。
-
urllib:Python标准库中的模块,提供了处理URL的功能,包括发送HTTP请求、处理响应等。虽然比较底层,但在Python中无需安装额外的包即可使用。包括urllib.request用于发送请求、urllib.parse用于解析URL等
-
http.client:Python标准库中的HTTP客户端模块,用于处理HTTP请求和响应。虽然比较底层,但可以用来编写自定义的HTTP客户端。
-
aiohttp:基于asyncio的异步HTTP客户端/服务器框架,适用于编写高性能的异步网络应用程序。它支持异步请求和响应处理,适用于需要处理大量并发请求的场景。
-
httpx:一个现代化的异步HTTP客户端,类似于Requests但支持异步请求和响应处理。它提供了更简洁的API和更好的性能。
-
treq:基于Twisted的异步HTTP客户端库,适用于Twisted网络框架的应用程序。它提供了Twisted中异步风格的API。
这些库各有特点和适用场景,可以根据项目需求和个人偏好选择合适的库来处理网络请求。其中requests
和urllib
是Python中处理HTTP请求中非常常用的库,文章将重点对比这2个库
2.requests
和urllib
对比
相同点:
-
功能:两者都提供了发送HTTP请求和处理响应的功能,可以处理GET、POST等常见的HTTP请求方法。
-
适用性:两者都适用于简单的HTTP请求场景,如获取网页内容、发送表单数据等。
不同点:
-
API设计:
requests
提供了简洁、易用的API,更符合Pythonic风格,使用更加方便。urllib
的API相对较为底层,使用起来相对繁琐。
-
功能扩展:
requests
内置了丰富的功能,如会话管理、身份验证、重定向处理等,使用起来更加方便。urllib
功能相对较少,更多的功能需要通过urllib.request
模块的其他类和方法来实现,或者通过其他模块扩展。
-
性能和效率:
requests
的性能通常比urllib
更好(但非绝对),且使用更加高效,适合于简单的HTTP请求和响应处理。
代码示例
requests库安装
pip install requests
相同点:
以获取网页内容为例:
使用requests
发送GET请求:
import requests
response = requests.get('https://api.github.com')
print(response.status_code)
print(response.json())
使用urllib
发送GET请求:
import urllib.request
response = urllib.request.urlopen('https://api.github.com')
print(response.status)
print(response.read().decode('utf-8'))
两者输出的内容是完全一致的,两者通过简单的代码实现了相同的功能
不同点:
- API设计
使用requests
发送POST请求:
import requests
data = {'key': 'value'}
response = requests.post('https://httpbin.org/post', data=data)
print(response.text)
使用urllib
发送POST请求:
import urllib.parse
import urllib.request
data = urllib.parse.urlencode({'key': 'value'}).encode('utf-8')
req = urllib.request.Request('https://httpbin.org/post', data=data)
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))
从示例中可以看出,使用requests
发送HTTP请求更为简洁一些
- 功能扩展
使用requests
处理重定向:
import requests
response = requests.get('https://github.com')
print(response.url)
使用urllib
处理重定向:
import urllib.request
response = urllib.request.urlopen('https://github.com')
print(response.geturl())
使用requests
发送带有头部信息的请求:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get('https://www.example.com', headers=headers)
print(response.text)
使用urllib
发送带有头部信息的请求:
import urllib.request
req = urllib.request.Request('https://www.example.com', headers={'User-Agent': 'Mozilla/5.0'})
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))
使用requests
处理Cookie:
import requests
response = requests.get('https://httpbin.org/cookies/set', cookies={'cookie_name': 'cookie_value'})
print(response.cookies)
使用urllib
处理Cookie:
import http.cookiejar
import urllib.request
cookie_handler = urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())
opener = urllib.request.build_opener(cookie_handler)
response = opener.open('https://httpbin.org/cookies/set?cookie_name=cookie_value')
for cookie in cookie_handler.cookiejar:
print(cookie)
- 性能和效率
使用requests
发送大量请求:
import requests
import time
start_time = time.time()
for _ in range(10):
response = requests.get('https://www.example.com')
time.sleep(0.1)
print("requests elapsed time:", time.time() - start_time)
使用urllib
发送大量请求:
import urllib.request
import time
start_time = time.time()
for _ in range(10):
response = urllib.request.urlopen('https://www.example.com')
time.sleep(0.1)
print("urllib elapsed time:", time.time() - start_time)
这些示例展示了requests
和urllib
在API设计、功能扩展以及性能和效率等方面的不同之处。通过比较这些示例,你可以更加深入地了解它们之间的区别。