python模块之requests学习
来源:互联网 发布:动画软件有哪些 编辑:程序博客网 时间:2024/05/22 13:39
# -*- coding:utf-8 -*-# -*- auther:StarryTeng -*-'''discription:Requests is the only Non-GMO HTTP library for Python, safe for human consumption.Beloved FeaturesRequests is ready for today's web.Keep-Alive & Connection PoolingInternational Domains and URLsSessions with Cookie PersistenceBrowser-style SSL VerificationAutomatic Content DecodingBasic/Digest AuthenticationElegant Key/Value CookiesAutomatic DecompressionUnicode Response BodiesHTTP(S) Proxy SupportMultipart File UploadsStreaming DownloadsConnection TimeoutsChunked Requests.netrc Supportinstallpip install requestseasy_install requests'''####Python 3, the new best practice, is here to stay. Python 2 will retire in only 30 months!####import requestsimport jsonurl = 'https://api.github.com/user'url = 'http://httpbin.org/post'payload = {'key1': 'value1', 'key2': 'value2'}user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'fileDir = 'file.txt'headers = {'content-type': 'application/json','User-Agent': user_agent}data = json.dumps(payload)#requests 是post,get,delete,put,options,head上传result = requests.post(url, auth=('tengxing7452@163.com', 'testpassword'), params=payload, headers=headers, data=data,timeout=1)#timeout 仅对连接过程有效,与响应体的下载无关。#print result.status_code#print result.json()#print result.text#print result.encoding#print result.cookies#----------------------------------------------------------------------#requests 是支持流式上传files ={'file':open(fileDir,'rb')}result1 = requests.post(url, auth=('tengxing7452@163.com', 'testpassword'), files=files)#print result1.text#----------------------------------------------------------------------#如果想获取来自服务器的原始套接字响应,可以取得 r.raw 。 不过需要在初始请求中设置 stream=True 。#r1 = requests.get('https://github.com/timeline.json', stream=True)#----------------------------------------------------------------------#session回话sess = requests.Session()sess.get('http://httpbin.org/cookies/set/sessioncookie/123456789')result2 = sess.get("http://httpbin.org/get",headers=headers)#print(result2.text)#print result2.headers.keys()sess.headers.setdefault('set',"get")sess.headers.update({"User-Agent":"tengxing"})result2 = sess.put("http://httpbin.org/put")#print result2.textresult2 = sess.get('http://httpbin.org/headers', headers={'set': None})#print result2.text#----------------------------------------------------------------------#SSL证书 (跳过刚才 12306 的证书验证,把 verify 设置为 False 即可)#result3 = requests.get('https://kyfw.12306.cn/otn/', verify=True)#print result3.text#----------------------------------------------------------------------#代理'''export HTTP_PROXY="http://10.10.1.10:3128"export HTTPS_PROXY="http://10.10.1.10:1080"'''proxies = { #pip install requests[socks] 'http': 'socks5://user:pass@host:port', 'https': 'socks5://user:pass@host:port'}proxies = { 'http': 'http://user:pass@10.10.1.10:3128/', 'https': 'http://user:pass@10.10.1.10:3128/'}result4_1 = requests.post("http://httpbin.org/post")print result4_1.textresult4_2 = requests.post("http://httpbin.org/post", proxies=proxies)print result4_2.text#最后感谢链接:http://docs.python-requests.org/en/master/user/advanced/
感谢官方文档:http://docs.python-requests.org/en/master/user/advanced/
阅读全文
1 0
- python模块之requests学习
- Python模块学习 - Requests
- python requests 模块学习
- python爬虫之urllib模块和requests模块学习
- Python requests模块的学习
- python中的requests模块学习
- Python requests模块学习笔记
- python-requests模块学习与扩充
- Python 爬虫学习笔记一: requests 模块
- python发送http请求之requests模块
- 每天一个python模块之Requests
- Python网络操作之requests模块
- 每天一个python模块之Requests
- Python之Requests模块使用详解
- Python之Requests模块使用详解
- 每天一个python模块之Requests
- Python爬虫Requests模块系列之六
- python发送http请求之requests模块
- NOIP2017 赛前模拟(2017.10.6)
- XLSTAT.2017.Premium.v19.5.47062.Win32_64 1CD
- 从键盘输入6个学生5门课程的成绩,求每个人的总分与平均分。
- WiFi认证过程及AP与STA连接关系的获取
- 常见五种排序算法PHP
- python模块之requests学习
- Android View框架的draw机制
- Hibernate实现分页的小案例
- 【状态压缩 && 思维转换】Codeforces Round #438 868C Qualification Rounds
- MySQL数据库小技巧(注意点)
- 20171006-Zookeeper
- 关于ubuntu下安装exlipse(和jdk)
- 使用composer搭建zendframework2框架
- WisdomTool REST Client V1.1支持自动化测试RESTful API并自动生成RESTful API文档