如何使用 Boto3 获取 AWS 账户中所有爬虫的列表


在本文中,我们将了解用户如何获取AWS账户中所有现有爬虫的列表。

示例

问题陈述: 使用 Python 中的 boto3 库获取所有爬虫的列表。

解决此问题的方法/算法

  • 步骤 1: 导入 boto3botocore 异常以处理异常。

  • 步骤 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}}

更新于:2021年4月15日

391 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.