如何使用Boto3获取AWS账户中所有网络爬虫的列表
本文将介绍用户如何获取AWS账户中所有网络爬虫的列表。
阅读更多:Python 教程
示例
问题陈述: 使用Python中的 boto3 库获取所有网络爬虫的列表。
解决这个问题的方法/算法
- 步骤1: 导入 boto3 和 botocore 异常以处理异常。
-
步骤2: 该函数没有参数。
-
步骤3: 使用 boto3 库创建AWS会话。请确保在默认配置文件中提到了 region_name 。如果没有提到,则在创建会话时明确传递 region_name 。
-
步骤4: 创建客户端 glue AWS 。
-
步骤5: 现在使用 list_crawlers
-
步骤6: 它返回AWS Glue数据目录中存在的所有网络爬虫的列表。
-
步骤7: 如果检查作业时出现错误,则处理通用异常。
代码示例
以下代码获取所有网络爬虫的列表 −
import boto3
from botocore.exceptions import ClientError
def list_of_crawlers()
session = boto3.session.Session()
glue_client = session.client('glue')
try:
crawler_details = glue_client.list_crawlers()
return crawler_details
except ClientError as e:
raise Exception("boto3 client error in list_of_crawlers: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in list_of_crawlers: " + e.__str__())
print(list_of_crawlers())
输出
{'CrawlerNames': ['crawler_for_s3_file_job', 'crawler_for_employee_data', 'crawler_for_security_data'], 'ResponseMetadata': {'RequestId': 'a498ba4a-7ba4-47d3-ad81-d86287829c1d', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 13 Feb 2021 14:04:03 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '830', 'connection': 'keep-alive', 'x-amzn-requestid': 'a498ba4a-7ba4-47d3-ad81-d86287829c1d'}, 'RetryAttempts': 0}}
极客教程