使用grequests发布json数据?

时间:2015-04-23 11:31:26

标签: python python-3.x grequests

我需要使用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']密钥的值更改为我将要发布的许多POST的不同网址。

所以我正在做:

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

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

当我将请求映射到回复时:

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模块中一样放入标题。

我设置标头kwarg参数使编码为none,以及content-type:

(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'}

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

2 个答案:

答案 0 :(得分:4)

您在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) 有多个值。我想你的意思是:

$(document).ready(function() {
  $('.bluetext').on('click', function(event) {
    var url = window.location.href.split("?");
    alert(url[1]);
  });
});

答案 1 :(得分:3)

您正在尝试发送 JSON 数据;让data为您编码(并设置正确的内容类型),使用myrequests = (grequests.post(POST_URL, json=fgp(a_url)) for a_url in all_urls) 参数而不是/^\d{1,3}-\d{0,7}-\d{0,2}|^\d{1,3}-\d{0,7}|^\d{1,3}/

$(function() {
    var timeOut = 0;
    $("#test").on('keyup blur', function() {
         var e =$(this);
        // cancel looking, the user typed another character
        clearTimeout(timeOut);
        // set a timeout, when user doesn't type another key
        // within the time set, the function is called
		timeOut = setTimeout(function() {
			stopptedTyping(e);
		}, 300); // change time as needed
    });

});

function stopptedTyping(e){      
  e.val( e.val().match(/^\d{1,3}-\d{0,7}-\d{0,2}|^\d{1,3}-\d{0,7}|^\d{1,3}/));
}