zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python的HTTP客户端库requests使用示例详解编程语言

Python客户端HTTP编程语言 使用 详解 示例 requests
2023-06-13 09:20:24 时间

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] 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/8396.html

cjavapython