Python——request模块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python——request模块相关的知识,希望对你有一定的参考价值。

1.Requsets模块的安装

进入cmd

cd C:\Python27\Scripts
pip 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=payload)
11 #data不光可以接受字典类型的数据,还可以接受json等格式
12 import json
13 payload = {a:,b:hello}
14 r = requests.post(http://httpbin.org/post, data=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.encoding
39 r.encoding = utf-8
40 
41 #json处理
42 r = requests.get(https://github.com/timeline.json)
43 print r.json() #需要先import json    
44 
45 #定制请求头
46 url = http://m.ctrip.com
47 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.headers
50 
51 #复杂post请求
52 url = http://m.ctrip.com
53 payload = {some: data}
54 r = requests.post(url, data=json.dumps(payload)) #如果传递的payload是string而不是dict,需要先调用dumps方法格式化一下
55 
56 #post多部分编码文件
57 url = http://m.ctrip.com
58 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_code
64     
65 #响应头
66 r = requests.get(http://m.ctrip.com)
67 print r.headers
68 print r.headers[Content-Type]
69 print r.headers.get(content-type) #访问响应头部分内容的两种方式
70     
71 #Cookies
72 r = requests.get(http://www.baidu.com)
73 r.cookies[BAIDUID]  #读取cookies
74     
75 url = http://m.ctrip.com/cookies
76 cookies = dict(cookies_are=working)
77 r = requests.get(url, cookies=cookies) #发送cookies
78 
79 #设置超时时间
80 r = requests.get(http://m.ctrip.com, timeout=0.001)
81 #访问中使用session
82 s = requests.Session() #先初始化一个session对象
83 r = s.post(url,data = 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:CN
20                }
21     r = requests.post("http://www.xxxxxx.com/CityLocation/json/LBSLocateCity",headers=headers,data=payload)
22     #r.encoding = ‘utf-8‘
23     data=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的value
27     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.asmx
14     r = requests.post(url,headers=headers,data=XML)
15     #r.encoding = ‘utf-8‘
16     data = r.text
17     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=payload,headers=header)
17 print r.status_code

6.快速口算 分值: 350

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

 

以上是关于Python——request模块的主要内容,如果未能解决你的问题,请参考以下文章

入门学Python一定要知道的requests模块安装及使用

request模块

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

你如何在 python 中处理 graphql 查询和片段?

python笔记8:requests模块