文章目录
- 用于发送 HTTP/HTTPS 请求,获取网页内容、下载文件等。
- urllib.request.urlopen(url, data=None, timeout=None):打开 URL,返回响应对象。 url:URL 字符串或 Request 对象。 data:POST 请求的数据(需为字节类型)。 timeout:超时时间(秒)。 urllib.request.Request(url, data=None, headers={}):创建自定义请求对象,支持添加头信息。 urllib.request.urlretrieve(url, filename=None):下载 URL 内容到本地文件。
- import urllib.request # 发送 GET 请求 with urllib.request.urlopen(“https://api.github.com”) as response: content = response.read().decode(“utf-8”) print(content[:100]) # 输出: GitHub API 响应(JSON 格式)
- import urllib.request import urllib.parse # 准备 POST 数据 data = urllib.parse.urlencode({“name”: “Alice”, “age”: 30}).encode(“utf-8”) req = urllib.request.Request(“https://httpbin.org/post”, data=data, method=”POST”) with urllib.request.urlopen(req) as response: print(response.read().decode(“utf-8”)) # 输出: POST 数据响应
- import urllib.request urllib.request.urlretrieve(“https://example.com/image.jpg”, “image.jpg”) print(“File downloaded”)
- 处理网络请求中的异常。
- URLError:URL 相关错误(如网络连接失败、域名无效)。 HTTPError:HTTP 状态码错误(如 404、500),是 URLError 的子类。
- import urllib.request import urllib.error try: with urllib.request.urlopen(“https://example.com/nonexistent”) as response: print(response.read().decode(“utf-8″)) except urllib.error.HTTPError as e: print(f”HTTP Error: {e.code} – {e.reason}”) # 输出: HTTP Error: 404 – Not Found except urllib.error.URLError as e: print(f”URL Error: {e.reason}”) # 输出: URL 相关错误
- 用于解析、构造和编码 URL。
- urllib.parse.urlparse(url):解析 URL 为组件(如协议、主机、路径)。 urllib.parse.urlunparse(components):从组件构造 URL。 urllib.parse.urlencode(query):将字典编码为查询字符串。 urllib.parse.quote(string):对字符串进行 URL 编码。 urllib.parse.unquote(string):解码 URL 编码的字符串。
- import urllib.parse url = “https://example.com/path?name=Alice&age=30#section” parsed = urllib.parse.urlparse(url) print(parsed) # 输出: ParseResult(scheme=’https’, netloc=’example.com’, path=’/path’, params=”, query=’name=Alice&age=30′, fragment=’section’)
- import urllib.parse query = {“name”: “Alice”, “age”: 30} encoded = urllib.parse.urlencode(query) print(encoded) # 输出: name=Alice&age=30 # 构造完整 URL url = f”https://example.com?{encoded}” print(url) # 输出: https://example.com?name=Alice&age=30
- import urllib.parse path = “path with spaces” encoded = urllib.parse.quote(path) print(encoded) # 输出: path%20with%20spaces print(urllib.parse.unquote(encoded)) # 输出: path with spaces
- 用于解析网站的 robots.txt 文件,检查爬虫是否允许访问特定 URL。
- 使用 urllib.request 获取网页内容,结合 urllib.parse 处理 URL。 示例: import urllib.request import urllib.parse base_url = “https://httpbin.org/get” params = urllib.parse.urlencode({“q”: “python”}) url = f”{base_url}?{params}” with urllib.request.urlopen(url) as response: print(response.read().decode(“utf-8”)) # 输出: JSON 响应
- 发送 GET 或 POST 请求调用 REST API。 示例(调用 GitHub API): import urllib.request import json req = urllib.request.Request( “https://api.github.com/users/octocat”, headers={“Accept”: “application/json”} ) with urllib.request.urlopen(req) as response: data = json.loads(response.read().decode(“utf-8”)) print(data[“login”]) # 输出: octocat
- 使用 urlretrieve 下载文件。 示例: import urllib.request urllib.request.urlretrieve(“https://www.python.org/static/img/python-logo.png”, “python_logo.png”)
- 使用 urllib.robotparser 确保爬虫符合网站规则。 示例: import urllib.robotparser rp = urllib.robotparser.RobotFileParser(“https://python.org/robots.txt”) rp.read() print(rp.can_fetch(“MyBot”, “/dev”)) # 检查是否允许爬取
目录
- 引言
- 1. urllib 模块简介
- 1.1 主要特点
- 1.2 安装
- 1.3 导入
- 2. urllib 的子模块和功能
- 2.1 urllib.request
- 核心功能
- 示例(简单 GET 请求)
- 示例(POST 请求)
- 示例(下载文件)
- 2.2 urllib.error
- 常见异常
- 示例(异常处理)
- 2.3 urllib.parse
- 核心功能
- 示例(解析 URL)
- 示例(构造查询字符串)
- 示例(URL 编码)
- 2.4 urllib.robotparser
- 核心功能
- 示例
- 3. 实际应用场景
- 3.1 网页爬取
- 3.2 API 调用
- 3.3 文件下载
- 3.4 检查爬虫权限
- 4. 最佳实践
- 5. 注意事项
- 6. 总结
在 Python 中,urllib 是一个标准库模块,用于处理 URL(统一资源定位符)相关的操作,包括发送 HTTP 请求、解析 URL、处理查询参数以及管理 URL 编码等。urllib 模块由多个子模块组成,提供了从基础到高级的网络功能,适用于爬虫、API 调用、文件下载等场景。虽然 urllib 功能强大,但对于复杂任务,开发者可能更倾向于使用第三方库如 requests。
以下是对 Python urllib 模块的详细介绍,包括其子模块、功能、用法、示例、应用场景、最佳实践和注意事项。
urllib 模块是 Python 标准库的一部分(无需额外安装),主要用于处理网络请求和 URL 操作。它由以下四个子模块组成:
urllib.request:用于发送 HTTP/HTTPS 请求,获取网络资源。urllib.error:定义网络请求相关的异常(如 HTTP 错误、URL 错误)。urllib.parse:用于解析和操作 URL(如拆分、编码查询参数)。urllib.robotparser:用于解析robots.txt文件,检查爬虫权限。
- 标准库:无需安装,适合轻量级网络任务。
- 功能全面:支持 HTTP/HTTPS 请求、URL 解析、查询参数编码、爬虫规则检查。
- 跨平台:在 Linux、macOS、Windows 上运行一致。
- 基础性:适合简单场景,复杂任务可结合
requests 或 aiohttp。
requests 或 aiohttp。
urllib 是 Python 标准库的一部分,支持 Python 2.7 和 3.x(本文以 Python 3.9+ 为例)。
import urllib.request
import urllib.error
import urllib.parse
import urllib.robotparser
以下详细介绍 urllib 的四个子模块及其核心功能。
用于发送 HTTP/HTTPS 请求,获取网页内容、下载文件等。
urllib.request.urlopen(url, data=None, timeout=None):打开 URL,返回响应对象。
url:URL 字符串或 Request 对象。
data:POST 请求的数据(需为字节类型)。
timeout:超时时间(秒)。
urllib.request.Request(url, data=None, headers={}):创建自定义请求对象,支持添加头信息。
urllib.request.urlretrieve(url, filename=None):下载 URL 内容到本地文件。
urllib.request.urlopen(url, data=None, timeout=None):打开 URL,返回响应对象。
url:URL 字符串或Request对象。data:POST 请求的数据(需为字节类型)。timeout:超时时间(秒)。
urllib.request.Request(url, data=None, headers={}):创建自定义请求对象,支持添加头信息。urllib.request.urlretrieve(url, filename=None):下载 URL 内容到本地文件。
import urllib.request
# 发送 GET 请求
with urllib.request.urlopen("https://api.github.com") as response:
content = response.read().decode("utf-8")
print(content[:100]) # 输出: GitHub API 响应(JSON 格式)
import urllib.request
import urllib.parse
# 准备 POST 数据
data = urllib.parse.urlencode({"name": "Alice", "age": 30}).encode("utf-8")
req = urllib.request.Request("https://httpbin.org/post", data=data, method="POST")
with urllib.request.urlopen(req) as response:
print(response.read().decode("utf-8")) # 输出: POST 数据响应
import urllib.request
urllib.request.urlretrieve("https://example.com/image.jpg", "image.jpg")
print("File downloaded")
处理网络请求中的异常。
URLError:URL 相关错误(如网络连接失败、域名无效)。
HTTPError:HTTP 状态码错误(如 404、500),是 URLError 的子类。
URLError:URL 相关错误(如网络连接失败、域名无效)。HTTPError:HTTP 状态码错误(如 404、500),是 URLError 的子类。
import urllib.request
import urllib.error
try:
with urllib.request.urlopen("https://example.com/nonexistent") as response:
print(response.read().decode("utf-8"))
except urllib.error.HTTPError as e:
print(f"HTTP Error: {e.code} - {e.reason}") # 输出: HTTP Error: 404 - Not Found
except urllib.error.URLError as e:
print(f"URL Error: {e.reason}") # 输出: URL 相关错误
用于解析、构造和编码 URL。
urllib.parse.urlparse(url):解析 URL 为组件(如协议、主机、路径)。
urllib.parse.urlunparse(components):从组件构造 URL。
urllib.parse.urlencode(query):将字典编码为查询字符串。
urllib.parse.quote(string):对字符串进行 URL 编码。
urllib.parse.unquote(string):解码 URL 编码的字符串。
urllib.parse.urlparse(url):解析 URL 为组件(如协议、主机、路径)。urllib.parse.urlunparse(components):从组件构造 URL。urllib.parse.urlencode(query):将字典编码为查询字符串。urllib.parse.quote(string):对字符串进行 URL 编码。urllib.parse.unquote(string):解码 URL 编码的字符串。
import urllib.parse
url = "https://example.com/path?name=Alice&age=30#section"
parsed = urllib.parse.urlparse(url)
print(parsed)
# 输出: ParseResult(scheme='https', netloc='example.com', path='/path', params='', query='name=Alice&age=30', fragment='section')
import urllib.parse
query = {"name": "Alice", "age": 30}
encoded = urllib.parse.urlencode(query)
print(encoded) # 输出: name=Alice&age=30
# 构造完整 URL
url = f"https://example.com?{encoded}"
print(url) # 输出: https://example.com?name=Alice&age=30
import urllib.parse
path = "path with spaces"
encoded = urllib.parse.quote(path)
print(encoded) # 输出: path%20with%20spaces
print(urllib.parse.unquote(encoded)) # 输出: path with spaces
用于解析网站的 robots.txt 文件,检查爬虫是否允许访问特定 URL。
RobotFileParser:解析 robots.txt 文件。
can_fetch(user_agent, url):检查指定用户代理是否允许访问 URL。
RobotFileParser:解析 robots.txt 文件。can_fetch(user_agent, url):检查指定用户代理是否允许访问 URL。
import urllib.robotparser
rp = urllib.robotparser.RobotFileParser()
rp.set_url("https://example.com/robots.txt")
rp.read()
print(rp.can_fetch("*", "https://example.com/allowed")) # 输出: True 或 False
使用 urllib.request 获取网页内容,结合 urllib.parse 处理 URL。
示例:
import urllib.request
import urllib.parse
base_url = "https://httpbin.org/get"
params = urllib.parse.urlencode({"q": "python"})
url = f"{base_url}?{params}"
with urllib.request.urlopen(url) as response:
print(response.read().decode("utf-8")) # 输出: JSON 响应
发送 GET 或 POST 请求调用 REST API。
示例(调用 GitHub API):
import urllib.request
import json
req = urllib.request.Request(
"https://api.github.com/users/octocat",
headers={"Accept": "application/json"}
)
with urllib.request.urlopen(req) as response:
data = json.loads(response.read().decode("utf-8"))
print(data["login"]) # 输出: octocat
使用 urlretrieve 下载文件。
示例:
import urllib.request
urllib.request.urlretrieve("https://www.python.org/static/img/python-logo.png", "python_logo.png")
使用 urllib.robotparser 确保爬虫符合网站规则。
示例:
import urllib.robotparser
rp = urllib.robotparser.RobotFileParser("https://python.org/robots.txt")
rp.read()
print(rp.can_fetch("MyBot", "/dev")) # 检查是否允许爬取
始终处理异常:
- 使用
try-except捕获HTTPError和URLError。 - 示例:
try:
urllib.request.urlopen("https://invalid-url")
except urllib.error.URLError as e:
print(f"Failed: {e}")
使用上下文管理器:
- 使用
with语句确保响应对象正确关闭。 - 示例:
with urllib.request.urlopen("https://example.com") as response:
content = response.read()
设置请求头:
- 添加
User-Agent和其他头信息,避免被服务器拒绝。 - 示例:
req = urllib.request.Request(
"https://example.com",
headers={"User-Agent": "Mozilla/5.0"}
)
参数化 URL:
- 使用
urllib.parse.urlencode构造查询参数。 - 示例:
params = urllib.parse.urlencode({"q": "python tutorial"})
url = f"https://example.com/search?{params}"
测试网络操作:
- 使用
pytest测试请求和解析逻辑,结合unittest.mock模拟响应。 - 示例:
import pytest
from unittest.mock import patch
def test_urlopen():
with patch("urllib.request.urlopen") as mocked:
mocked.return_value.__enter__.return_value.read.return_value = b"mocked data"
with urllib.request.urlopen("https://example.com") as response:
assert response.read() == b"mocked data"
考虑使用 requests:
- 对于复杂任务(如会话管理、JSON 解析),考虑使用
requests库。 - 示例:
import requests
response = requests.get("https://api.github.com")
print(response.json())
版本要求:
urllib在 Python 3.x 中分为子模块,Python 2 的urllib和urllib2已合并。- 示例(Python 2 兼容):
# Python 2
import urllib2
response = urllib2.urlopen("https://example.com")
编码处理:
urllib.request返回字节数据,需手动解码(如decode("utf-8"))。urllib.parse.urlencode要求数据为字符串,POST 数据需编码为字节。- 示例:
data = urllib.parse.urlencode({"key": "value"}).encode("utf-8")
超时设置:
- 始终设置
timeout参数,避免请求挂起。 - 示例:
urllib.request.urlopen("https://example.com", timeout=5)
性能问题:
urllib.request适合简单任务,复杂场景(如并发请求)使用aiohttp或httpx。- 示例(异步请求):
import aiohttp
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.get("https://example.com") as response:
return await response.text()
安全性:
- 使用 HTTPS 协议,避免明文传输。
- 验证 SSL 证书,防止中间人攻击:
import ssl
context = ssl.create_default_context()
urllib.request.urlopen("https://example.com", context=context)
Python 的 urllib 模块是处理 URL 和网络请求的标准库工具,包含四个子模块:
urllib.request:发送 HTTP/HTTPS 请求,下载文件。urllib.error:处理请求异常。urllib.parse:解析和编码 URL。urllib.robotparser:解析robots.txt。
其核心特点包括:
- 简单易用:适合轻量级网络任务。
- 应用场景:网页爬取、API 调用、文件下载、爬虫规则检查。
- 最佳实践:异常处理、上下文管理器、设置请求头、参数化 URL。
虽然 urllib 功能强大,但对于复杂场景(如会话管理、异步请求),建议使用 requests 或 aiohttp。
以上就是Python使用urllib模块处理网络请求和URL的操作指南的详细内容,更多关于Python urllib处理网络请求和URL的资料请关注风君子博客其它相关文章!
您可能感兴趣的文章:
- Python网络请求模块urllib与requests使用介绍
- python 包实现 urllib 网络请求操作
- Python基础请求库urllib模块使用深入探究
- Python爬虫请求模块Urllib及Requests库安装使用教程
- Python中的urllib库高级用法教程