如何使用 mturk 遍历 boto3 中的结果

How can iterate through results in boto3 with mturk

我是 boto 的新手,我尝试遍历我能得到的结果。

特别是,我想计算所有具有给定资格的​​工人。但是,限制是 100,我不明白它如何与 NextToken 一起使用。有人能帮帮我吗?

# next_token = 1
qualification_count = 0
while True:
    response = s3.list_workers_with_qualification_type(
        QualificationTypeId=qualification_id,
        Status='Granted',
        MaxResults=100,
        NextToken=next_token
    )
    next_token = response['NextToken']
    qualification_count += response['NumResults']

显然 next_token 不正确,但我不知道它应该是什么

有几件事可能会把你搞砸。第一个是次要的,即您正在使用的客户端名为 s3。这可能只是您为 MTurk 选择的变量名称,但值得确保您没有尝试针对 AWS S3 客户端调用它。

第二个是您在第一次调用 While 循环时引用 next_token(变量)。问题是它不会在您第一次通过时进行初始化,因此注定会失败。同样,这可能只是您展示的简短代码片段的遗留问题,而不是实际问题。

但无论哪种方式,以下代码都应该有效。请注意,您可以将页面大小配置为 return(我相信最多 100)。但重要的是,它永远不会传递未初始化的 NextToken,并且它会正确设置 MTurk 客户端。这段代码对我有效。如果您 运行 对此有任何问题,请告诉我。很乐意进一步提供帮助。

import boto3

region_name = 'us-east-1'
aws_access_key_id = 'YOUR_ACCESS_KEY'
aws_secret_access_key = 'YOUR_SECRET_KEY'

PAGE_SIZE = 20

endpoint_url = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'

client = boto3.client('mturk',
    endpoint_url = endpoint_url,
    region_name = region_name,
    aws_access_key_id = aws_access_key_id,
    aws_secret_access_key = aws_secret_access_key,
)

qualification_id='9W4ZQKNWM3FZ5HGM2070'

response = client.list_workers_with_qualification_type(
        QualificationTypeId=qualification_id,
        Status='Granted',
        MaxResults=PAGE_SIZE
    )
next_token = response['NextToken']

qualification_count = response['NumResults']
while (response['NumResults'] == PAGE_SIZE):
    print "Using next token of {}".format(next_token)
    response = client.list_workers_with_qualification_type(
            QualificationTypeId=qualification_id,
            Status='Granted',
            MaxResults=PAGE_SIZE,
            NextToken=next_token
    )
    next_token = response['NextToken']
    qualification_count += response['NumResults']

print "There are {} Workers in Qualification {}".format(qualification_count, qualification_id)