Python requests模块实例用法
Python  /  管理员 发布于 7年前   407
1、Requests模块说明
Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想。
Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API 太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。
在Python的世界里,事情不应该这么麻烦。
Requests 使用的是 urllib3,因此继承了它的所有特性。Requests 支持 HTTP 连接保持和连接池,支持使用 cookie 保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。现代、国际化、人性化。
2、Requests模块安装
点此下载
然后执行安装
$ python setup.py install
个人推荐使用pip安装
pip install requests
也可以使用easy_install安装
easy_install requests
尝试在IDE中import requests,如果没有报错,那么安装成功。
3、Requests模块简单入门
#HTTP请求类型#get类型r = requests.get('https://github.com/timeline.json')#post类型r = requests.post("http://m.ctrip.com/post")#put类型r = requests.put("http://m.ctrip.com/put")#delete类型r = requests.delete("http://m.ctrip.com/delete")#head类型r = requests.head("http://m.ctrip.com/head")#options类型r = requests.options("http://m.ctrip.com/get")#获取响应内容print r.content #以字节的方式去显示,中文显示为字符print r.text #以文本的方式去显示#URL传递参数payload = {'keyword': '日本', 'salecityid': '2'}r = requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload) print r.url #示例为http://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword=日本#获取/修改网页编码r = requests.get('https://github.com/timeline.json')print r.encodingr.encoding = 'utf-8'#json处理r = requests.get('https://github.com/timeline.json')print r.json() #需要先import json #定制请求头url = 'http://m.ctrip.com'headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}r = requests.post(url, headers=headers)print r.request.headers#复杂post请求url = 'http://m.ctrip.com'payload = {'some': 'data'}r = requests.post(url, data=json.dumps(payload)) #如果传递的payload是string而不是dict,需要先调用dumps方法格式化一下#post多部分编码文件url = 'http://m.ctrip.com'files = {'file': open('report.xls', 'rb')}r = requests.post(url, files=files)#响应状态码r = requests.get('http://m.ctrip.com')print r.status_code #响应头r = requests.get('http://m.ctrip.com')print r.headersprint r.headers['Content-Type']print r.headers.get('content-type') #访问响应头部分内容的两种方式 #Cookiesurl = 'http://example.com/some/cookie/setting/url'r = requests.get(url)r.cookies['example_cookie_name'] #读取cookies url = 'http://m.ctrip.com/cookies'cookies = dict(cookies_are='working')r = requests.get(url, cookies=cookies) #发送cookies#设置超时时间r = requests.get('http://m.ctrip.com', timeout=0.001)#设置访问代理proxies = { "http": "http://10.10.10.10:8888", "https": "http://10.10.10.100:4444", }r = requests.get('http://m.ctrip.com', proxies=proxies)
xml请求
#!/user/bin/env python#coding=utf-8import requestsclass url_request(): def __init__(self): """ init """ if __name__=='__main__': headers = {'Content-type': 'text/xml'} XML = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><Request xmlns="http://tempuri.org/"><jme><JobClassFullName>WeChatJSTicket.JobWS.Job.JobRefreshTicket,WeChatJSTicket.JobWS</JobClassFullName><Action>RUN</Action><Param>1</Param><HostIP>127.0.0.1</HostIP><JobInfo>1</JobInfo><NeedParallel>false</NeedParallel></jme></Request></soap:Body></soap:Envelope>' url = 'http://jobws.push.mobile.xxxxxxxx.com/RefreshWeiXInTokenJob/RefreshService.asmx' r = requests.post(url,headers=headers,data=XML) #r.encoding = 'utf-8' data = r.text print data
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号