在Python爬虫开发中,处理异常是非常重要的,因为它可以帮助你确保程序在遇到错误时不会崩溃,并且可以记录或报告错误信息。以下是一些常见的异常处理方法:
-
使用
try-except
语句: 这是处理异常的基本方法。你可以将可能引发异常的代码放在try
块中,然后在except
块中捕获并处理异常。try: # 可能引发异常的代码 response = requests.get(url) response.raise_for_status() # 如果响应状态码不是200,会引发HTTPError异常 except requests.exceptions.HTTPError as http_err: print(f"HTTP error occurred: {http_err}") except Exception as err: print(f"An error occurred: {err}") else: # 如果没有异常发生,执行这里的代码 data = https://www.yisu.com/ask/response.json()>
-
使用
try-except-finally
语句:finally
块中的代码无论是否发生异常都会执行。这对于清理资源(如关闭文件、数据库连接等)非常有用。try: # 可能引发异常的代码 response = requests.get(url) response.raise_for_status() except requests.exceptions.HTTPError as http_err: print(f"HTTP error occurred: {http_err}") except Exception as err: print(f"An error occurred: {err}") else: # 如果没有异常发生,执行这里的代码 data = https://www.yisu.com/ask/response.json()"Finished processing.")
-
使用日志记录: 日志记录可以帮助你更好地跟踪和调试程序。你可以使用Python的
logging
模块来记录异常信息。import logging logging.basicConfig(filename='app.log', level=logging.ERROR) try: # 可能引发异常的代码 response = requests.get(url) response.raise_for_status() except requests.exceptions.HTTPError as http_err: logging.error(f"HTTP error occurred: {http_err}") except Exception as err: logging.error(f"An error occurred: {err}") else: # 如果没有异常发生,执行这里的代码 data = https://www.yisu.com/ask/response.json()>
-
使用
try-except
语句处理特定类型的异常: 如果你只想捕获特定类型的异常,可以在except
块中指定异常类型。try: # 可能引发异常的代码 response = requests.get(url) response.raise_for_status() except requests.exceptions.Timeout as timeout_err: print(f"Timeout error occurred: {timeout_err}") except requests.exceptions.RequestException as req_err: print(f"Request error occurred: {req_err}") except Exception as err: print(f"An error occurred: {err}") else: # 如果没有异常发生,执行这里的代码 data = https://www.yisu.com/ask/response.json()>
通过这些方法,你可以有效地处理Python爬虫开发中的异常,确保程序的稳定性和可靠性。