Python JSON 迭代到 POST 请求

Python JSON Iteration into a POST request

我有一个 JSON 格式类似于

的文件
{
   "unknown1": 
   [
        {"text": "random text again",
        "time": "Thu May 15 19:21:59 +0000 2016"}, 
        "text": "akmfkdlm safsa fasffalmfa",
        "time": "Thu May 21 09:53:51 +0000 2016"}
   ]
   "unknown2":
   [ 
        "text": "fsda lmfalmfa",
        "time": "Thu May 21 09:53:51 +0000 2016"},
   ]
}

JSON 中的第一项是 运行dom(未知)标签,这些未知数可以有任意数量。在这些未知数中总是有一堆 text/time 配对。

我正在尝试将每个 text 发送到我的 REST post 服务中,该服务接受 JSON 格式为

text: "foo bar bat",
mime_type: "text/html",
extract_type: "HP" # HP, MP

因此,当我尝试 运行 我的代码时出现此错误,但我不确定该怎么做。

这是我的代码:

import json
import requests

with open('locations_stripped.json') as data_file:
    data = json.load(data_file)

headers = {'Content-Type' : 'application/json'}

for thing in data:
    for text, time in data.iteritems():
        print text
        body = [{ "text": text , "mime_type": "text/html", "extract_type": "HP"}]
        r = requests.post('localhost:3003/api/extract/run', data=body, headers=headers)
        print (r.content)

这里是错误:

 $ python filterrest.py
unknown1
Traceback (most recent call last):
  File "filterrest.py", line 30, in <module>
    r = requests.post('localhost:3003/api/extract/run', data=body, headers=headers)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 111, in post
return request('post', url, data=data, json=json, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 57, in request
return session.request(method=method, url=url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 461, in request
prep = self.prepare_request(req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 394, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 298, in prepare
self.prepare_body(data, files, json)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 452, in prepare_body
body = self._encode_params(data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 89, in _encode_params
for k, vs in to_key_val_list(data):
ValueError: too many values to unpack

需要注意的一件事是它打印了错误的文本("unknown1" 而不是 "random text again"),我不确定如何让它只打印文本。

有什么帮助吗?

更新

每个人的 answer/comment 我更改了我的代码

... 
for thing in data:
    for text in data[thing]:
        print text['text']

这会打印文本['text'],正如我所期望的那样。问题在于我执行请求的方式。我更改了我的代码作为测试并将数据设置为我知道应该工作的东西(我通过 Postman 运行)。

更改代码:

r = requests.post('localhost:3003/api/extract/run', data='Hello. Where does the brown fox go?', headers=headers)

预期响应:

[
  {
    "score": 0.30253747367501777,
    "tag": "HP",
  }
]

相反,打印的是看起来像整个 HTML 页面的内容。

假设您有一个有效的json。首先需要遍历"unknown"键对应的列表,现在这个列表又包含了字典 使用 text time 键。

for unknown_key in data:
    for obj in data[unknown_key]:
         body = { "text": obj['text'] , "mime_type": "text/html", "extract_type": "HP"}
         r = requests.post('localhost:3003/api/extract/run', data=body, headers=headers)
         print (r.content)

关于您问题的第 1 部分:

for thing in data:
    for text, time in data.iteritems():

用这个循环你不会得到 text。您的更新:

for thing in data:
    for text in data[thing]:
        print text['text']

是正确的。你的 headers 是对的。下一个问题是:

body = [{ "text": text , "mime_type": "text/html", "extract_type": "HP"}]
r = requests.post('localhost:3003/api/extract/run', data=body, headers=headers)

现在查看模块requests:

的文档

Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made

There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.

对于关键字参数 data,您必须提供 dict 或有效的 json str。您在问题中的变量 body 直到 list 并且在您的更新中是无效的 json str。有2种解决方案:

body = { "text": text , "mime_type": "text/html", "extract_type": "HP"}
# Dont't forget: dict will be used to send form-encoded data
# It will work. But not a intended solution for json data
r = requests.post('localhost:3003/api/extract/run', data=body, headers=headers)

或者

import json
body = { "text": text , "mime_type": "text/html", "extract_type": "HP"}
r = requests.post('localhost:3003/api/extract/run', data=json.dumps(body), headers=headers)

但是 requests 文档说:

Instead of encoding the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically

所以从2.4.2版本开始,最好使用关键字参数json代替data发送json数据。所以这是最好的解决方案:

body = { "text": text , "mime_type": "text/html", "extract_type": "HP"} 
r = requests.post('localhost:3003/api/extract/run', json=body, headers=headers)

总结

我使用这个来源:

{
   "unknown1": 
   [
        {"text": "random text again",
        "time": "Thu May 15 19:21:59 +0000 2016"}, 
        "text": "akmfkdlm safsa fasffalmfa",
        "time": "Thu May 21 09:53:51 +0000 2016"}
   ],
   "unknown2":
   [ 
        "text": "fsda lmfalmfa",
        "time": "Thu May 21 09:53:51 +0000 2016"},
   ]
}

代码:

import json
import requests

with open('locations_stripped.json') as data_file:
    data = json.load(data_file)

headers = {'Content-Type' : 'application/json'}

for list_values in data.values():
    for dict_element in list_values:
        text = dict_element['text']
        print text
        body = { "text": text , "mime_type": "text/html", "extract_type": "HP"}
        r = requests.post('localhost:3003/api/extract/run', json=body, headers=headers)
        print (r.content)

P/S: 我不知道你的服务器,所以没法测试。我希望,它有效。