【python库对比】网络专题-requests和urllib对比

文章概览:
1. 简单介绍网络请求常用的python库
2. 对比Requests和urllib的异同
3. 结合代码示例说明两个库的差异

1. 简介

Python中处理网络请求的相关库有很多,其中一些常用的包括:

  1. requests:一个简洁且功能强大的HTTP库,用于发送HTTP请求和处理响应。它是Python中最常用的网络请求库之一,使用简单,易于上手。

  2. urllib:Python标准库中的模块,提供了处理URL的功能,包括发送HTTP请求、处理响应等。虽然比较底层,但在Python中无需安装额外的包即可使用。包括urllib.request用于发送请求、urllib.parse用于解析URL等

  3. http.client:Python标准库中的HTTP客户端模块,用于处理HTTP请求和响应。虽然比较底层,但可以用来编写自定义的HTTP客户端。

  4. aiohttp:基于asyncio的异步HTTP客户端/服务器框架,适用于编写高性能的异步网络应用程序。它支持异步请求和响应处理,适用于需要处理大量并发请求的场景。

  5. httpx:一个现代化的异步HTTP客户端,类似于Requests但支持异步请求和响应处理。它提供了更简洁的API和更好的性能。

  6. treq:基于Twisted的异步HTTP客户端库,适用于Twisted网络框架的应用程序。它提供了Twisted中异步风格的API。

这些库各有特点和适用场景,可以根据项目需求和个人偏好选择合适的库来处理网络请求。其中requestsurllib是Python中处理HTTP请求中非常常用的库,文章将重点对比这2个库

2.requestsurllib对比

相同点:

  1. 功能:两者都提供了发送HTTP请求和处理响应的功能,可以处理GET、POST等常见的HTTP请求方法。

  2. 适用性:两者都适用于简单的HTTP请求场景,如获取网页内容、发送表单数据等。

不同点:

  1. API设计

    • requests提供了简洁、易用的API,更符合Pythonic风格,使用更加方便。
    • urllib的API相对较为底层,使用起来相对繁琐。
  2. 功能扩展

    • requests内置了丰富的功能,如会话管理、身份验证、重定向处理等,使用起来更加方便。
    • urllib功能相对较少,更多的功能需要通过urllib.request模块的其他类和方法来实现,或者通过其他模块扩展。
  3. 性能和效率

    • 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'))

两者输出的内容是完全一致的,两者通过简单的代码实现了相同的功能
在这里插入图片描述

不同点:

  1. 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请求更为简洁一些

  1. 功能扩展
    使用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)
  1. 性能和效率

使用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)

这些示例展示了requestsurllib在API设计、功能扩展以及性能和效率等方面的不同之处。通过比较这些示例,你可以更加深入地了解它们之间的区别。

  • 46
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值