python post 请求json文件_post 请求获取 json

不建议自己用正则处理,基本所有的编程语言里都有json处理的库,比如在python中,可以这样做

import json

import urllib2

data = json.load(urllib2.urlopen("https://api.github.com/users/defunkt"))

上面的代码 使用get请求github的api,当然也可以使用post方法,可以使用requests这个库,更加方便

结果

{u'public_repos': 101, u'site_admin': True, u'subscriptions_url': u'https://api.github.com/users/defunkt/subscriptions', u'gravatar_id': u'b8dbb1987e8e5318584865f880036796', u'hireable': True, u'id': 2, u'followers_url': u'https://api.github.com/users/defunkt/followers', u'following_url': u'https://api.github.com/users/defunkt/following{/other_user}', u'blog': u'http://chriswanstrath.com/', u'followers': 12921, u'location': u'San Francisco', u'type': u'User', u'email': u'chris@github.com', u'bio': u'', u'gists_url': u'https://api.github.com/users/defunkt/gists{/gist_id}', u'company': u'GitHub', u'events_url': u'https://api.github.com/users/defunkt/events{/privacy}', u'html_url': u'https://github.com/defunkt', u'updated_at': u'2013-12-22T08:59:46Z', u'received_events_url': u'https://api.github.com/users/defunkt/received_events', u'starred_url': u'https://api.github.com/users/defunkt/starred{/owner}{/repo}', u'public_gists': 284, u'name': u'Chris Wanstrath', u'organizations_url': u'https://api.github.com/users/defunkt/orgs', u'url': u'https://api.github.com/users/defunkt', u'created_at': u'2007-10-20T05:24:19Z', u'avatar_url': u'https://gravatar.com/avatar/b8dbb1987e8e5318584865f880036796?d=https%3A%2F%2Fidenticons.github.com%2Fc81e728d9d4c2f636f067f89cc14862c.png&r=x', u'repos_url': u'https://api.github.com/users/defunkt/repos', u'following': 208, u'login': u'defunkt'}

>>> data["login"]

u'defunkt'

>>>

如果使用requests模块

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.post("http://httpbin.org/post", data=payload)

>>> print r.text

{

...

"form": {

"key2": "value2",

"key1": "value1"

},

...

}

顺便说下 http://httpbin.org 这个网站可以用来测试,非常方便

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值