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 に対して、キーの値を別の URLに変更する必要があります。
だから私はやっている:
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 引数を設定して、エンコーディングをなしにし、コンテンツ タイプを作成します。
(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'}
注: 一部のデータを非表示にするために出力を少し編集しました。