这篇文章主要简介了python调用接口的4种方式代码实例,文中通过示例代码简介的非常具体,对大家的学习或者工作具有一定的参考学习网上卖,需要的朋友可以学习下
python中调用API的几种方式:
- urllib2
- httplib2
- pycurl
- requests
1.urllib2
import urllib2, urllib
github_url =
'https://api.github.com/user/repos'
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None,
github_url, 'user', '***')
auth = urllib2.HTTPBasicAuthHandler(
password_manager)# create an authentication handler
opener = urllib2.build_opener(auth)# create an opener with the authentication handler
urllib2.install_opener(opener)# install the opener
...
request = urllib2.Request(github_url,
urllib.urlencode({
'name': 'Test repo',
'description': 'Some test rep