Python在做数据抓取的时候,经常会用到重试,最简单的就是while true
,不过代码略显复杂,今天来介绍一些流行的库可以用来实现重试机制,每种库都有其特点和适用场景。以下是一些常用的重试框架:
tenacity
tenacity
是一个非常流行的重试库。tenacity
提供了一种灵活的方式来配置重试策略,包括重试次数、重试间隔、停止条件等。
安装命令:
pip install tenacity
使用案例:
import requests
from bs4 import BeautifulSoup
from tenacity import retry, stop_after_attempt, wait_fixed
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
def fetch_page(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
response = requests.get(url, headers=headers)
response.raise_for_status() # This will raise an HTTPError for bad responses (4xx and 5xx)
return response.text
def parse_page(html):
soup = BeautifulSoup(html, 'html.parser')
titles = [title.get_text() for title in soup.find_all('h1', class_='article-title')]
return titles
def main():
url = 'http://example.com' # Replace with your target URL
try:
html = fetch_page(url)
results = parse_page(html)
print(results)
except Exception as e:
print(f"Failed to fetch the page: {e}")
if __name__ == '__main__':
main()
@retry
装饰器来包装fetch_page
函数。
stop_after_attempt(3)
表示最多重试3次,
而wait_fixed(2)
则意味着每次重试之间等待2秒钟。
response.raise_for_status()
会在遇到HTTP错误时抛出异常,
这些异常会被tenacity
捕获并触发重试。
-
retrying
retrying
是一个功能强大且易于使用的库,它提供了灵活的重试策略,如固定等待、随机等待、指数退避等。它还支持指定特定异常类型的重试。安装命令:
pip install retrying
使用案例:
from retrying import retry
import requests
# 使用装饰器添加重试逻辑
# wait_fixed: 指定每次重试前的固定等待时间(毫秒)
# stop_max_attempt_number: 最大重试次数(包括首次尝试)
@retry(wait_fixed=1000, stop_max_attempt_number=3)
def fetch_url(url):
response = requests.get(url)
response.raise_for_status() # 如果状态码不是200,则抛出HTTPError
return response.text
url = 'http://example.com'
try:
content = fetch_url(url)
print(content[:100])
except requests.exceptions.HTTPError as e:
print(f"HTTP error occurred: {e}")
except requests.exceptions.ConnectionError as e:
print(f"Connection error occurred: {e}")
except requests.exceptions.Timeout as e:
print(f"Timeout error occurred: {e}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
-
backoff
backoff
库专注于优雅的重试和指数退避策略,特别适用于网络请求和其他可能暂时失败的操作。它允许你通过装饰器轻松地添加重试逻辑到函数上。安装命令:
pip install backoff
使用案例:
import backoff
import requests
# 使用装饰器添加重试逻辑
# on_exception: 指定当遇到哪些类型的异常时执行重试
# expo: 指定指数退避策略,即重试间隔随重试次数增加而呈指数增长
# max_tries: 最大重试次数(包括首次尝试)
@backoff.on_exception(backoff.expo, requests.exceptions.RequestException, max_tries=3)
def fetch_url(url):
response = requests.get(url)
response.raise_for_status() # 如果状态码不是200,则抛出HTTPError
return response.text
url = 'http://example.com'
try:
content = fetch_url(url)
print(content[:100])
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
-
retry
retry
库提供了一个简单的装饰器来实现重试功能,支持指定重试次数和等待时间。虽然它的功能相对较少,但代码量小,易于理解。安装命令:
pip install retry
使用案例:
import retry
import requests
# 使用装饰器添加重试逻辑
# tries: 最大重试次数(包括首次尝试)
# delay: 初始重试延迟时间(秒)
# backoff: 每次重试后的延迟时间乘以此值,实现指数退避
@retry.retry(tries=3, delay=1, backoff=2)
def fetch_url(url):
response = requests.get(url)
response.raise_for_status() # 如果状态码不是200,则抛出HTTPError
return response.text
url = 'http://example.com'
try:
content = fetch_url(url)
print(content[:100])
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
-
resilient
resilient
是一个旨在提高代码弹性和容错性的库,其中包含了重试机制。它不仅提供了重试功能,还支持熔断器和限流器,适合构建高可用的系统。安装命令:
pip install resilient
使用案例:
from resilient import CircuitBreaker, RetryPolicy
import requests
# 创建熔断器实例
# failure_threshold: 当连续失败次数达到此阈值时,熔断器打开
# recovery_timeout: 熔断器打开后的恢复时间(秒)
circuit_breaker = CircuitBreaker(failure_threshold=3, recovery_timeout=30)
# 创建重试策略实例
# max_retries: 最大重试次数(不包括首次尝试)
# retry_delay: 两次重试之间的延迟时间(秒)
retry_policy = RetryPolicy(max_retries=3, retry_delay=1)
# 使用熔断器和重试策略装饰器
@circuit_breaker
@retry_policy
def fetch_url(url):
response = requests.get(url)
response.raise_for_status() # 如果状态码不是200,则抛出HTTPError
return response.text
url = 'http://example.com'
try:
content = fetch_url(url)
print(content[:100])
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
选择哪个库取决于你的具体需求,比如你是否需要复杂的重试策略、是否需要与其他弹性组件(如熔断器)集成,以及你对库大小和依赖的考虑。tenacity
因其灵活性和广泛的功能集而成为许多人的首选,但其他库也有其独特的优势。在选择之前,建议查阅每个库的文档,了解其特性和使用案例,以找到最适合你项目需求的工具。