python的HTTP客户端库requests使用示例,pythonrequests,requests是pyt


requests是python的一个HTTP客户端库,和urllib、urllib2类似,但是urllib2的api比较复杂,比如像实现一个post或是get功能都得需要一大堆代码。

    # -*- coding:utf8 -*-      import request      r = requests.get('http://www.zhidaow.com')#发送请求      r.status_code#返回状态码      r.headers['content-type']#返回头部信息      r.encoding#返回编码信息      r.text#返回内容部分      #各种HTTP请求      r = requests.post('http://httpbin.org/post')      r = requests.put('http://httpbin.org/put')      r = requests.delete('http://httpbin.org/delete')      r = requests.head('http://httpbin.org/get')      r = requests.options('http://httpbin.org/get')      #带有参数的请求例子      payload = {'wd':'张亚楠','rn':'100'}      r = requests.get('http://www.baidu.com/s',parama = payload)      print r.url      #获取json结果      r = requests.get('...')      r.join()['data']['country']  

评论关闭