Skip to main content
 首页 » 编程设计

python之使用 grequests 发布 json 数据

2025年05月04日13tuyile006

我需要使用 grequests 发出异步 POST 请求。

我的帖子正文(json 格式)是这样的:

[{'params': {'source': 'widget', 
   'id': 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif', 
   'groupId': '@self', 
   'nolog': 'true', 
   'userId': '@viewer'}, 
  'method': 'pos.plusones.get', 
  'id': 'p', 
  'jsonrpc': '2.0', 
  'apiVersion': 'v1', 
  'key': 'p'}] 

我需要将 [0]['params']['id'] 键的值更改为不同的 URL,以便我创建许多 POST。

所以我在做:

myrequests = (grequests.post(POST_URL, data=fgp(a_url) for a_url in all_urls)

我的生成器理解中的 fgp() 方法是一种将 [0]['params']['id'] 更改为 的方法a_url 传递给它,在我发送的 POST 正文中。

当我将请求映射到响应时:

myresponses = grequests.map(myrequests)

这就是我得到的,有多少次就有多少次请求(很明显)。

Traceback (most recent call last): 
  File "/home/ashk/.virtualenvs/cosignp3/src/gevent/gevent/greenlet.py", line 340, in run 
    result = self._run(*self.args, **self.kwargs) 
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/grequests.py", line 71, in send 
    self.url, **merged_kwargs) 
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/sessions.py", line 451, in request 
    prep = self.prepare_request(req) 
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/sessions.py", line 382, in prepare_request 
    hooks=merge_hooks(request.hooks, self.hooks), 
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/models.py", line 307, in prepare 
    self.prepare_body(data, files, json) 
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/models.py", line 456, in prepare_body 
    body = self._encode_params(data) 
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/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 (expected 2) 
<Greenlet at 0x7f0f7cbf33d8: <bound method AsyncRequest.send of <grequests.AsyncRequest object at 0x7f0f7c90d080>>(stream=False)> failed with ValueError 

编辑:问题已解决:-

我开始尝试,并像在 requests 模块中一样放入 header 。

我正在设置 headers kwarg 参数使编码无,内容类型:

(grequests.post(POST_URL, data=fgp(a_url, j=True), headers={'Accept-Encoding':'none', 'Content-Type':'application/json'}) for a_url in urls) 

现在我得到了正确的输出:

In [64]: resps[0].json() 
Out[64]:  
{'result': {'isSetByViewer': False, 
  'kind': 'pos#plusones', 
  'metadata': {'type': 'URL', 'globalCounts': {'count': 0.0 }}, 
  'id': 'http://example.com/dfg', 
  'abtk': 'xxxxxxxxxxxxxxx'}, 
 'id': 'p'} 

注意:对输出进行了一些编辑以隐藏一些数据。

请您参考如下方法:

您在显示的 grequests.post 行中缺少一个闭包。但是,假设它实际上位于该行的末尾:

myrequests = (grequests.post(POST_URL, data=fgp(a_url) for a_url in all_urls)) 

这意味着您只生成了一个 post 调用,但为 data 生成了多个值。我认为你的意思是:

myrequests = (grequests.post(POST_URL, data=fgp(a_url)) for a_url in all_urls)