通过MTurk API从沙箱获取HIT结果时HIT不存在的解决方法
解决MTurk沙箱中无法查看HIT结果(提示HIT不存在)的问题
嘿,我一眼就发现问题所在了——你在查询结果的时候误用了HITGroupId,而不是每个HIT独有的HITId!让我一步步帮你解决:
问题回顾
你已经成功用boto3在MTurk沙箱发布了HIT,Worker能看到并提交了响应,但调用list_assignments_for_hit时系统提示“HIT不存在”。
你使用的发布HIT代码:
import boto3 MTURK_SANDBOX = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com' MTURK_PROD = 'https://mturk-requester.us-east-1.amazonaws.com' mturk = boto3.client('mturk', aws_access_key_id = "blah", aws_secret_access_key = "blah", region_name='us-east-1', endpoint_url = MTURK_SANDBOX ) # Read in the questions.xml file saved in the same directory question = open(file='E:\\QuestionsXML\\5235319d5aa41337122b28bc.xml',mode='r', encoding='utf-8').read() new_hit = mturk.create_hit( Title = 'Map the following question to the paragraph/content where the answer to the question may lie.', Description = 'Please review the following content to figure out where the answer to the posted question may reside', Keywords = 'images, quick, labeling', Reward = '0.00', MaxAssignments = 1, LifetimeInSeconds = 1728, AssignmentDurationInSeconds = 600, AutoApprovalDelayInSeconds = 14400, Question = question, AssignmentReviewPolicy={ 'PolicyName':'ScoreMyKnownAnswers/2011-09-01', 'Parameters':[ {'Key':'AnswerKey', 'MapEntries':[{'Key': 'question_1', 'Values':['yes']}]}, {'Key': 'ApproveIfKnownAnswerScoreIsAtLeast', 'Values':['1']}, {'Key': 'RejectIfKnownAnswerScoreIsLessThan', 'Values':['1']}, {'Key': 'RejectReason', 'Values':['Sorry, we could not approve your submission as you did not correctly identify the image containing the Flamingo.']}, {'Key': 'ExtendIfKnownAnswerScoreIsLessThan','Values':['1']} ] }) print("A new HIT has been created. You can preview it here:") print(new_hit['HIT']['HITGroupId']) print("https://workersandbox.mturk.com/mturk/preview?groupId=" + str(new_hit['HIT']['HITGroupId']))
代码输出:
A new HIT has been created. You can preview it here: 3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB https://workersandbox.mturk.com/mturk/preview?groupId=3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB
查询结果的代码:
mturk = boto3.client('mturk', aws_access_key_id = "blah", aws_secret_access_key = "blah", region_name='us-east-1', endpoint_url = MTURK_SANDBOX ) # You will need the following library # to help parse the XML answers supplied from MTurk # Install it in your local environment with # pip install xmltodict import xmltodict # Use the hit_id previously created hit_id = '3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB' # We are only publishing this task to one Worker # So we will get back an array with one item if it has been completed worker_results = mturk.list_assignments_for_hit(HITId=hit_id, AssignmentStatuses=['Submitted'])
报错信息:
--------------------------------------------------------------------------- RequestError Traceback (most recent call last) <ipython-input-22-e82852bb5415> in <module>() 14 # We are only publishing this task to one Worker 15 # So we will get back an array with one item if it has been completed ---> 16 worker_results = mturk.list_assignments_for_hit(HITId=hit_id, AssignmentStatuses=['Submitted']) ~\Anaconda3\lib\site-packages\botocore\client.py in _api_call(self, *args, **kwargs) 312 "%s() only accepts keyword arguments." % py_operation_name) 313 # The "self" in this scope is referring to the BaseClient. --> 314 return self._make_api_call(operation_name, kwargs) 315 316 _api_call.__name__ = str(py_operation_name) ~\Anaconda3\lib\site-packages\botocore\client.py in _make_api_call(self, operation_name, api_params) 610 error_code = parsed_response.get("Error", {}).get("Code") 611 error_class = self.exceptions.from_code(error_code) --> 612 raise error_class(parsed_response, operation_name) 613 else: 614 return parsed_response RequestError: An error occurred (RequestError) when calling the ListAssignmentsForHIT operation: Hit 3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB does not exist. (1526664623533 s)
问题根源
你在查询时用的3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB是HITGroupId,它是一组相同HIT的标识(比如批量发布相同任务时的组ID),而list_assignments_for_hit需要的是每个HIT独有的HITId——这两个是完全不同的参数!
解决方案
1. 发布HIT时获取并记录正确的HITId
修改发布代码的打印部分,把HITId也打印出来:
print("A new HIT has been created. You can preview it here:") print("HIT ID:", new_hit['HIT']['HITId']) # 新增:打印唯一的HITId print("HIT Group ID:", new_hit['HIT']['HITGroupId']) print("https://workersandbox.mturk.com/mturk/preview?groupId=" + str(new_hit['HIT']['HITGroupId']))
运行后,你会得到类似这样的输出,其中HIT ID后面的值就是你需要的:
A new HIT has been created. You can preview it here: HIT ID: 123ABC456DEF789GHI012JKL345MNO678PQR HIT Group ID: 3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB https://workersandbox.mturk.com/mturk/preview?groupId=3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB
2. 使用正确的HITId查询结果
把查询代码中的hit_id替换成刚才得到的HITId:
# 替换成你实际的HITId hit_id = '123ABC456DEF789GHI012JKL345MNO678PQR' worker_results = mturk.list_assignments_for_hit(HITId=hit_id, AssignmentStatuses=['Submitted'])
3. (可选)验证HIT是否存在
如果还是不确定,可以先调用get_hit接口确认HIT状态:
hit_details = mturk.get_hit(HITId=hit_id) print("HIT Status:", hit_details['HIT']['HITStatus'])
如果返回正常,说明HIT确实存在,接下来就可以正常获取Worker的响应了。
额外提示
- HITGroupId主要用于预览批量发布的HIT,或者管理一组相同的任务;而每个单独发布的HIT都有唯一的HITId,是查询任务详情、结果的关键标识。
- 如果你已经找不到之前的HITId,也可以调用
list_hits接口列出你所有的HIT,找到对应的那一个:
all_hits = mturk.list_hits() for hit in all_hits['HITs']: print("HIT ID:", hit['HITId'], "Title:", hit['Title'])
内容的提问来源于stack exchange,提问作者Patthebug
相关产品推荐
相关产品推荐

