将 JSON 对象传递给带有请求的 url [英] Pass a JSON object to an url with requests

查看:24
本文介绍了将 JSON 对象传递给带有请求的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想使用 Kenneth 出色的请求模块.在尝试使用 Freebase API 时偶然发现了这个问题.

基本上,他们的 API 如下所示:

https://www.googleapis.com/freebase/v1/mqlread?query=...

作为查询,他们期望一个 JSON 对象,这里有一个 返回葡萄酒列表他们的国家和酒精度百分比:

<代码>[{国家":空,名称":空,百分比酒精":空,百分比酒精>":0,类型":/食物/酒"}]

当然,在将其传递给 URL 之前,我们必须避免这种情况,因此实际查询将如下所示:

 fullurl = 'https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22percentage_alcohol%3E%22%3A+0%2C+%22country%22%3A+null%2C+%22type%22%3A+%22%2Ffood%2Fwine%22%2C+%22name%22%3A+null%2C+%22percentage_alcohol%22%3A+null%7D%5D'

现在,

r = requests.get(fullurl)打印 r.status_code>>>400

因为该站点声称它无法解析查询.

r2 = urllib2.urlopen(fullurl)打印 r2.getcode()>>>200

没问题,我得到了正确的回报.有趣的是,

#这是我们requests.get请求的url打印 urllib2.urlopen(r.url).getcode()>>>200

为什么?我使用的模块有误吗?还是 requests 中的错误?

解决方案

它对我有用.这是我所做的:

<预><代码>>>>参数 = [{国家":无,...名称":无,...百分比酒精":无,...百分比酒精>":0,...类型":/食物/酒"... }]>>>导入json>>>params_json = json.dumps(params)>>>进口请求>>>url = "https://www.googleapis.com/freebase/v1/mqlread?query=%s">>>r = requests.get(url % params_json)>>>r.status_code200>>>content_json = json.loads(r.content)>>>导入打印>>>pprint.pprint(content_json){u'result': [{u'country': u'新西兰',u'name': u'2003 Cloudy Bay Sauvignon Blanc',u'percentage_alcohol':13.5,u'type': u'/food/wine'},{u'country': u'France',你的名字:u'G.H.Mumm Cordon Rouge Brut',u'percentage_alcohol': 12.0,u'type': u'/food/wine'},....

为了简洁起见,我把其余的都删掉了.有 100 个结果.requests.__version__ == '0.10.6'

So, I want to use Kenneth' excellent requests module. Stumbled up this problem while trying to use the Freebase API.

Basically, their API looks like that:

https://www.googleapis.com/freebase/v1/mqlread?query=...

as a query, they expect a JSON object, here's one that will return a list of wines with their country and percentage of alcohol:

[{
  "country":       null,
  "name":          null,
  "percentage_alcohol": null,
  "percentage_alcohol>": 0,
  "type":          "/food/wine"
}]​

Of course, we'll have to escape the hell out of this before passing it to an URL, so the actual query will look like this:

 fullurl = 'https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22percentage_alcohol%3E%22%3A+0%2C+%22country%22%3A+null%2C+%22type%22%3A+%22%2Ffood%2Fwine%22%2C+%22name%22%3A+null%2C+%22percentage_alcohol%22%3A+null%7D%5D'

Now,

r = requests.get(fullurl)
print r.status_code
>>> 400

because the site claims it couldn't parse the query.

r2 = urllib2.urlopen(fullurl)
print r2.getcode()
>>> 200

No problem here, I get a proper return. Interestingly,

# This is the url of our requests.get request
print urllib2.urlopen(r.url).getcode() 
>>> 200

Why? Am I using the module wrong? Or is it a bug in requests?

解决方案

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'

这篇关于将 JSON 对象传递给带有请求的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆