You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HTML文件创建Amazon MTurk HIT时触发HTMLQuestion解析报错

问题原因

你直接将本地HTML文件的原始文本传入了create_hit接口的Question参数,MTurk API要求该参数必须传入符合官方XML Schema规范的结构化问答数据,不支持直接传入裸HTML内容,因此触发了找不到HTMLQuestion元素声明的解析错误。

修复方法

你需要将读取到的HTML内容包裹在MTurk规定的HTMLQuestion标准XML结构中,使用CDATA块包裹HTML内容避免特殊字符导致XML解析失败,再将拼接完成的完整XML字符串传给Question参数即可。

修复后的核心代码逻辑如下:

import boto3

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

mturk = boto3.client('mturk',
   aws_access_key_id = "替换为你的访问密钥ID",
   aws_secret_access_key = "替换为你的访问密钥",
   region_name='us-east-1',
   endpoint_url = MTURK_SANDBOX
)

questionfile = open("/home/nm6088/mturk files/prac4_Jun27/index.html","r", encoding="utf-8")
raw_html = questionfile.read()
questionfile.close()

# 按MTurk要求包裹HTML为标准Question XML格式
questions = f"""
<HTMLQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2011-11-11/HTMLQuestion.xsd">
  <HTMLContent><![CDATA[
{raw_html}
  ]]></HTMLContent>
  <FrameHeight>600</FrameHeight>
</HTMLQuestion>
"""

localRequirements = [{
    'QualificationTypeId': '00000000000000000071',
    'Comparator': 'EqualTo',
    'LocaleValues': [{'Country': 'US'}],
    'RequiredToPreview': True
}]    

hit = mturk.create_hit(
    Title='Write a simple version of the test',
    Description='A test HIT that requires the user to write a simple text.',
    Keywords='simple, qualification, test',
    Reward='0.01',
    MaxAssignments=1,
    LifetimeInSeconds=3600,
    AssignmentDurationInSeconds=600,
    AutoApprovalDelayInSeconds=200,
    Question = questions,
    QualificationRequirements=localRequirements
)
 
print ("A new HIT has been created. You can preview it here:")
print ("https://workersandbox.mturk.com/mturk/preview?groupId=" + hit['HIT']['HITGroupId'])
print ("HITID = " + hit['HIT']['HITId'] + " (Use to Get Results)") 
注意事项
  • 代码里的FrameHeight参数可以根据你HTML页面的实际高度调整,单位是像素,避免HIT内容被截断显示不全
  • 不要把AWS访问密钥硬编码在代码中,建议通过本地AWS凭证配置文件或者环境变量传入,避免密钥泄露造成资产损失
  • 读取HTML文件时建议显式指定encoding="utf-8",避免中文或特殊字符出现乱码

内容的提问来源于stack exchange,提问作者Nadia Mehjabin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 11:21:43