我正在尝试使用uClassify API根据文本对对象进行分类。要与 API 交互,我需要发出 XML POST 请求,例如:
<?xml version="1.0" encoding="utf-8" ?>
<uclassify xmlns="http://api.uclassify.com/1/RequestSchema" version="1.01">
<writeCalls writeApiKey="YOUR_WRITE_API_KEY_HERE" classifierName="ManOrWoman">
<create id="CreateManOrWoman"/>
</writeCalls>
</uclassify>
我尝试使用 HTTP 请求模块以及 xml.etree.ElementTree 来创建 XML 树,但我得到了左右错误。这是我尝试过的一些代码:
>>> import elementtree.ElementTree as ET
>>> from xml.etree.cElementTree import Element, ElementTree
>>> import requests
>>>
>>> root = ET.Element("uclassify", xlms="http://api.uclassify.com/1/RequestSchema", version="1.01")
>>> head = ET.SubElement(root, "writeCalls", writeApiKey="*************", classifierName="test")
>>> action = ET.SubElement(head, "create", id="CreateTest")
>>> tree = ElementTree(root)
>>>
>>> r = requests.post('http://api.uclassify.com/', tree)
>>>
>>> ........
>>> TypeError: must be convertible to a buffer, not ElementTree