Pass a JSON object to an url with requests

偶尔善良 提交于 2019-12-05 08:14:11

It works for me. Here's what I did:

>>> params = [{"country": None,
...            "name": None,
...            "percentage_alcohol": None,
...            "percentage_alcohol>": 0,
...            "type": "/food/wine"
...          }]
>>> import json
>>> params_json = json.dumps(params)

>>> import requests
>>> url = "https://www.googleapis.com/freebase/v1/mqlread?query=%s"
>>> r = requests.get(url % params_json)
>>> r.status_code
200

>>> content_json = json.loads(r.content)
>>> import pprint
>>> pprint.pprint(content_json)
{u'result': [{u'country': u'New Zealand',
              u'name': u'2003 Cloudy Bay Sauvignon Blanc',
              u'percentage_alcohol': 13.5,
              u'type': u'/food/wine'},
             {u'country': u'France',
              u'name': u'G.H. Mumm Cordon Rouge Brut',
              u'percentage_alcohol': 12.0,
              u'type': u'/food/wine'},
....

I cut the rest off for brevity. There are 100 results. requests.__version__ == '0.10.6'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!