首页 > 代码库 > Python——request模块

Python——request模块

1.Requsets模块的安装

进入cmd

cd C:\Python27\Scriptspip install requests

2.Requests模块的简单使用

 1 #HTTP请求类型 2 #无参数的get类型 3 r = requests.get(http://httpbin.org/get) 4 #有参的get类型 5 payload = {key1:vaule1,key2:value2} 6 r = requests.get(url,params=payload) 7 #无参的post类型 8 r = requests.post("http://httpbin.org/post") 9 #有参的post类型10 r = requests.post(url,data=http://www.mamicode.com/payload)11 #data不光可以接受字典类型的数据,还可以接受json等格式12 import json13 payload = {a:,b:hello}14 r = requests.post(http://httpbin.org/post, data=http://www.mamicode.com/json.dumps(payload))15 #发送文件的post类型,这个相当于向网站上传一张图片,文档等操作,这时要使用files参数16 files = {file: open(touxiang.png, rb)}17 r = requests.post(http://httpbin.org/post, files=files)18 #put类型19 r = requests.put("http://m.ctrip.com/put")20 #delete类型21 r = requests.delete("http://m.ctrip.com/delete")22 #head类型23 r = requests.head("http://m.ctrip.com/head")24 #options类型25 r = requests.options("http://m.ctrip.com/get")26 27 #获取响应内容28 print r.content #以字节的方式去显示,中文显示为字符29 print r.text #以文本的方式去显示30 31 #URL传递参数32 payload = {keyword: 日本, salecityid: 2}33 r = requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload) 34 print r.url #示例为http://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword=日本35 36 #获取/修改网页编码37 r = requests.get(https://github.com/timeline.json)38 print r.encoding39 r.encoding = utf-840 41 #json处理42 r = requests.get(https://github.com/timeline.json)43 print r.json() #需要先import json    44 45 #定制请求头46 url = http://m.ctrip.com47 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}48 r = requests.post(url, headers=headers)49 print r.request.headers50 51 #复杂post请求52 url = http://m.ctrip.com53 payload = {some: data}54 r = requests.post(url, data=http://www.mamicode.com/json.dumps(payload)) #如果传递的payload是string而不是dict,需要先调用dumps方法格式化一下55 56 #post多部分编码文件57 url = http://m.ctrip.com58 files = {file: open(report.xls, rb)}59 r = requests.post(url, files=files)60 61 #响应状态码62 r = requests.get(http://m.ctrip.com)63 print r.status_code64     65 #响应头66 r = requests.get(http://m.ctrip.com)67 print r.headers68 print r.headers[Content-Type]69 print r.headers.get(content-type) #访问响应头部分内容的两种方式70     71 #Cookies72 r = requests.get(http://www.baidu.com)73 r.cookies[BAIDUID]  #读取cookies74     75 url = http://m.ctrip.com/cookies76 cookies = dict(cookies_are=working)77 r = requests.get(url, cookies=cookies) #发送cookies78 79 #设置超时时间80 r = requests.get(http://m.ctrip.com, timeout=0.001)81 #访问中使用session82 s = requests.Session() #先初始化一个session对象83 r = s.post(url,data = http://www.mamicode.com/user) #然后使用这个session对象来进行访问84 #设置访问代理85 proxies = {86            "http": "http://10.10.10.10:8888",87            "https": "http://10.10.10.100:4444",88           }89 r = requests.get(http://m.ctrip.com, proxies=proxies)

3.json请求

 1 #!/user/bin/env python 2 #coding=utf-8 3 import requests 4 import json 5  6 class url_request(): 7     def __init__(self): 8             """ init """     9 10 if __name__==__main__:11     headers = {Content-Type : application/json}12     payload = {CountryName:中国,13                ProvinceName:陕西省,14                L1CityName:汉中,15                L2CityName:城固,16                TownName:‘‘,17                Longitude:107.33393,18                Latitude:33.157131,19                Language:CN20                }21     r = requests.post("http://www.xxxxxx.com/CityLocation/json/LBSLocateCity",headers=headers,data=http://www.mamicode.com/payload)22     #r.encoding = ‘utf-8‘23     data=http://www.mamicode.com/r.json()24     if r.status_code!=200:25         print "LBSLocateCity API Error " + str(r.status_code)26     print data[CityEntities][0][CityID] #打印返回json中的某个key的value27     print data[ResponseStatus][Ack]28     print json.dumps(data,indent=4,sort_keys=True,ensure_ascii=False) #树形打印json,ensure_ascii必须设为False否则中文会显示为unicode

4.xml请求

 1 #!/user/bin/env python 2 #coding=utf-8 3 import requests 4  5 class url_request(): 6     def __init__(self): 7             """ init """     8  9 if __name__==__main__:10     11     headers = {Content-type: text/xml}12     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>13     url = http://jobws.push.mobile.xxxxxxxx.com/RefreshWeiXInTokenJob/RefreshService.asmx14     r = requests.post(url,headers=headers,data=http://www.mamicode.com/XML)15     #r.encoding = ‘utf-8‘16     data =http://www.mamicode.com/ r.text17     print data

5.Requests模拟登入实例

 1 import requests 2  3 payload = {action:login:‘‘,uid:08133248,domain:cumt.edu.cn,nodetect:false,password:wattcswh,locale:zh_CN} 4 header = { 5 "Host": "mail.cumt.edu.cn", 6 "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0", 7 "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 8 "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3", 9 "Accept-Encoding": "gzip, deflate",10 "Referer": "http://acm.hdu.edu.cn/",11 "Cookie": "uid=08133248",12 "Connection": "keep-alive"13 }14 15 s = requests.Session()16 r = s.post(http://mail.cumt.edu.cn/coremail/index.jsp,data=http://www.mamicode.com/payload,headers=header)17 print r.status_code

6.快速口算 分值: 350

小明要参加一个高技能比赛,要求每个人都要能够快速口算四则运算,2秒钟之内就能够得到结果,但是小明就是一个小学生没有经过特殊的培训,那小明能否通过快速口算测验呢?
import retry:    import requestsexcept ImportError:    raise SystemExit(cuole)s = requests.Session()url=http://lab1.xseclab.com/xss2_0d557e6d2a4ac08b749b61473a075be1/index.phpr = s.get(url)res = unicode(r.content,utf-8).encode(gbk)num = re.findall(re.compile(r<br/>\s+(.*?)=),res)[0]r = s.post(url,data=http://www.mamicode.com/{v:eval(num)})key = re.findall(re.compile(r<body>(.*?)</body>), r.content)[0]open(d:/ans.txt,w).write(str(key))

 

Python——request模块