Python Request 库入门
In [4]:
import urllib2
import requests
import json
gh_url = 'https://api.github.com'
gh_user = 'gaufung'
gh_pw = 'gaofenggit123'
req = urllib2.Request(gh_url)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, gh_url, gh_user, gh_pw)
auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)
urllib2.install_opener(opener)
handler = urllib2.urlopen(req)
if handler.getcode() == requests.codes.ok:
text = handler.read()
d_text = json.loads(text)
for k, v in d_text.items():
print k, v
In [7]:
import requests
import json
gh_url = 'https://api.github.com'
gh_user = 'gaufung'
gh_pw = 'gaofenggit123'
r = requests.get(gh_url,auth=(gh_user,gh_pw))
if r.status_code == requests.codes.ok:
for k, v in r.json().items():
print k,v
In [12]:
import requests
cs_url = 'http://httpbin.org'
r = requests.get("%s/%s" % (cs_url, 'get'))
r = requests.post("%s/%s" % (cs_url, 'post'))
r = requests.put("%s/%s" % (cs_url, 'put'))
r = requests.delete("%s/%s" % (cs_url, 'delete'))
r = requests.patch("%s/%s" % (cs_url, 'patch'))
r = requests.options("%s/%s" % (cs_url, 'get'))
https://encrypted.google.com/search?q=hello
<协议>://<域名>/<接口>?<键1>=<值1>&<键2>=<值2>
requests 库提供的 HTTP 方法,都提供了名为 params 的参数。这个参数可以接受一个 Python 字典,并自动格式化为上述格式。
In [18]:
import requests
cs_url = 'https://www.so.com/s'
param = {'ie':'utf-8','q':'query'}
r = requests.get(cs_url,params = param)
print r.url
In [22]:
import requests
cs_url = 'https://www.zhihu.com'
r = requests.get(cs_url,timeout=100)
In [23]:
import requests
cs_url = 'http://httpbin.org/get'
r = requests.get (cs_url)
print r.content
通常我们比较关注其中的 User-Agent 和 Accept-Encoding。如果我们要修改 HTTP 头中的这两项内容,只需要将一个合适的字典参数传给 headers 即可。
In [24]:
import requests
my_headers = {'User-Agent' : 'From Liam Huang', 'Accept-Encoding' : 'gzip'}
cs_url = 'http://httpbin.org/get'
r = requests.get (cs_url, headers = my_headers)
print r.content
In [25]:
import requests
cs_url = 'http://httpbin.org/get'
r = requests.get (cs_url)
print r.headers
长期以来,互联网都存在带宽有限的情况。因此,网络上传输的数据,很多情况下都是经过压缩的。经由 requests 发送的请求,当收到的响应内容经过 gzip 或 deflate 压缩时,requests 会自动为我们解包。我们可以用 Response.content 来获得以字节形式返回的相应内容。
In [26]:
import requests
cs_url = 'https://www.zhihu.com'
r = requests.get (cs_url)
if r.status_code == requests.codes.ok:
print r.content
如果相应内容不是文本,而是二进制数据(比如图片),则需要进行响应的解码
In [27]:
import requests
from PIL import Image
from StringIO import StringIO
cs_url = 'http://liam0205.me/uploads/avatar/avatar-2.jpg'
r = requests.get (cs_url)
if r.status_code == requests.codes.ok:
Image.open(StringIO(r.content)).show()
文本模式解码
In [29]:
import requests
cs_url = 'https://www.zhihu.com'
r = requests.get (cs_url,auth=('gaofengcumt@126.com','gaofengcumt'))
if r.status_code == requests.codes.ok:
print r.text
else:
print 'bad request'
In [30]:
import requests
cs_url = 'http://ip.taobao.com/service/getIpInfo.php'
my_param = {'ip':'8.8.8.8'}
r = requests.get(cs_url, params = my_param)
print r.json()['data']['country'].encode('utf-8')
In [ ]: