将XML列表与请求一起发布到Elasticsearch?

2024-04-28 22:28:46 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法使用requests模块将XML列表发布到Elasticsearch? 列表如下所示:

[{'author': 'author name 1',
  'content': 'content',
  'title': 'title 1'},

 {'author': 'author name 2',
  'content': 'content',
  'title': 'title 2'},

 {'author': 'author name 3',
  'content': 'content',
  'title': 'title 3'}]

我有一个语料库,里面有我用etree解析的XML文件:

for file in corpus:
    tree = etree.parse(file)
    doc = {
    "author": tree.xpath('//tei:author/text()', namespaces=ns)[0],
    "title": tree.xpath('//tei:title/text()', namespaces=ns)[0],
    "content": "".join(tree.xpath('//tei:text/text()', namespaces=ns))
    }
    xmlList.append(doc)

我现在尝试使用以下请求将此发布到Elasticsearch:

response = requests.post('http://localhost:9200/test/test',
             json=xmlList) 

但这给了我一个错误的请求(400)。你知道吗


Tags: textnametree列表titlexmlteicontent