Python-requests库的学习与使用举例_python中request库学习实例,2024年最新最终入职阿里

58 阅读4分钟

def Get(): payload = {"lady": "killer", "key": "9"} headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/52.0.2743.116 Safari/537.36' } req = requests.get(url=URL_GET, params=payload, headers=headers) print("url:", req.url) print("状态码:", req.status_code) print("cookies", req.cookies) print("text:", req.text) print("json:", req.json()) req.close()


#### 结果




> 

> url: http://httpbin.org/get?lady=killer&key=9  

>  状态码: 200  

>  cookies <RequestsCookieJar[]>  

>  text: {  

>    "args": {  

>      "key": "9",  

>      "lady": "killer"  

>    },  

>    "headers": {  

>      "Accept": "\*/\*",  

>      "Accept-Encoding": "gzip, deflate",  

>      "Host": "httpbin.org",  

>      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10\_11\_4) AppleWebKit/537.36 (KHTML, like Gecko)    Chrome/52.0.2743.116 Safari/537.36",  

>      "X-Amzn-Trace-Id": "Root=1-5f8450e4-30d63b07549e5df637b48d4d"  

>    },  

>    "origin": "59.64.129.128",  

>    "url": "http://httpbin.org/get?lady=killer&key=9"  

>  }

> 

> 

> json: {'args': {'key': '9', 'lady': 'killer'}, 'headers': {'Accept': '\*/\*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': '  

>  Mozilla/5.0 (Macintosh; Intel Mac OS X 10\_11\_4) AppleWebKit/537.36 (KHTML, like Gecko)    Chrome/52.0.2743.116 Safari/537.36', 'X-Amzn-Trace-Id': 'Roo  

>  t=1-5f8450e4-30d63b07549e5df637b48d4d'}, 'origin': '59.64.129.128', 'url': 'http://httpbin.org/get?lady=killer&key=9'}

> 

> 

> 



实际访问的url已经添加了参数,也就是说,你可以不使用params参数,而是直接在url上面添加。


状态码请查看上面的文章


text是请求的结果


json格式是比较常用的




![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/205de487c58b46af807e3cf6d996015e~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MDgwNDUxMTkwMTI=:q75.awebp?rk3s=f64ab15b&x-expires=1771514136&x-signature=deaQ%2FiYrTRKwMyErUaOyz%2FatgZ4%3D)

 浏览器访问 
 



### POST请求



#### 函数




> 

> 

> ```

> post(url, data=None, json=None, **kwargs)

> ```

> 

> 



url:请求的url


data:请求时的数据,字典、元组列表、字节或类似文件


json:json数据



#### 代码



def Post(): data = {'bupt': 'beijing university of posts and communication', 'age': '65'} req = requests.post(URL_POST, data=data) print("url:", req.url) print("状态码:", req.status_code) print("cookies", req.cookies) print("text:", req.text) print("json:", req.json()) req.close()


#### 结果




> 

> url: http://httpbin.org/post  

>  状态码: 200  

>  cookies <RequestsCookieJar[]>  

>  text: {  

>    "args": {},  

>    "data": "",  

>    "files": {},  

>    "form": {  

>      "age": "65",  

>      "bupt": "beijing university of posts and communication"  

>    },  

>    "headers": {  

>      "Accept": "\*/\*",  

>      "Accept-Encoding": "gzip, deflate",  

>      "Content-Length": "57",  

>      "Content-Type": "application/x-www-form-urlencoded",  

>      "Host": "httpbin.org",  

>      "User-Agent": "python-requests/2.23.0",  

>      "X-Amzn-Trace-Id": "Root=1-5f845f63-5103e9761cd94b3b1c9905f3"  

>    },  

>    "json": null,  

>    "origin": "59.64.129.128",  

>    "url": "http://httpbin.org/post"  

>  }

> 

> 

> json: {'args': {}, 'data': '', 'files': {}, 'form': {'age': '65', 'bupt': 'beijing university of posts and communication'}, 'headers': {'Accept': '\*/\*', 'Accept-Encodin  

>  g': 'gzip, deflate', 'Content-Length': '57', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.23.0', 'X-Amzn  

>  -Trace-Id': 'Root=1-5f845f63-5103e9761cd94b3b1c9905f3'}, 'json': None, 'origin': '59.64.129.128', 'url': 'http://httpbin.org/post'}

> 

> 

> 




### 高级


有的时候我们往往需要登录才有权限发送请求,而账户信息一般会保存在Cookie中。



#### cookies设置


可以抓包获得cookie


也可以通过F12,在控制台输入document.cookie来获得。




![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/afb3513c230f4a4d8728b1996ed7108d~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MDgwNDUxMTkwMTI=:q75.awebp?rk3s=f64ab15b&x-expires=1771514136&x-signature=xJSiIRogOXH5q8uXnUi1h14KwOw%3D)

 浏览器查看 
 


代码



def Get_with_cookie(): headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/52.0.2743.116 Safari/537.36' } cookies = {'BIDUPSID': 'BE00784DEDAD866ECD4C82326FD62EAB', ' PSTM': '1554688108', ' MCITY': '-%3A', ' ispeed_lsm': '0', ' BAIDUID': '1588105DA2B8508F2F85540F7FC96277:FG', ' H_PS_PSSID': '', ' BDRCVFR[NPt2Vg_wYt_]': 'mk3SLVN4HKm', ' delPer': '0', ' BD_CK_SAM': '1', ' PSINO': '2', ' BD_UPN': '13314752', ' sug': '3', ' sugstore': '0', ' ORIGIN': '0', ' bdime': '0', ' H_PS_645EC': '6c6bmTTznHXowtzIVMcI9ybyItnOQcEc5yoROOp7O7Q4a0FQWI7CHETQrdW05P4yg3zuSFV3', ' BA_HECTOR': '24a4800l810k25k1ur1fo8obe0j', ' BDORZ': 'FFFB88E999055A3F8A630C64834BD6D0'} req = requests.get(URL_COOKIE, headers=headers, cookies=cookies) print("url:", req.url) print("状态码:", req.status_code) print("cookies", req.cookies) with open('ask_baidu_requets.html', 'w',encoding='utf-8') as f: f.write(req.text) req.close()


控制台输出



> 
> url: https://www.baidu.com/s?tn=02003390\_hao\_pg&ie=utf-8&wd=requests  
>  状态码: 200  
>  cookies <RequestsCookieJar[<Cookie BAIDUID=1588105DA2B8508F2F85540F7FC96277:FG=1 for .baidu.com/>, <Cookie BDRCVFR[NPt2Vg\_wYt\_]=mk3SLVN4HKm for .baidu.com/>, <Cookie H\_  
>  PS\_PSSID= for .baidu.com/>, <Cookie PSINO=2 for .baidu.com/>, <Cookie delPer=0 for .baidu.com/>, <Cookie BDSVRTM=12 for www.baidu.com/>, <Cookie BD\_CK\_SAM=1 for www.bai  
>  du.com/>]>
> 
> 
> 


生成的文件




![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/b0bba511c9f9490fb51875822b39bf6a~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MDgwNDUxMTkwMTI=:q75.awebp?rk3s=f64ab15b&x-expires=1771514136&x-signature=XPzMHUsEYg6qpW%2BaFpn8bhe75ws%3D)

 文件浏览器查看 
 


#### 超时设置


timeout参数


这个时间只限制请求的时间。


代码



def Get_with_timeout(): req = requests.get('github.com', timeout=0.001) print(req.text)


结果


![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/5c05d96807384241a96e829fb341a43c~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MDgwNDUxMTkwMTI=:q75.awebp?rk3s=f64ab15b&x-expires=1771514136&x-signature=9iVNbLv8XlknsazFWKx3ZH8FG48%3D)


## 全部代码



""" --coding:utf-8-- @File: learnrequests.py @Author:frank yu @DateTime: 2020.10.12 17:10 @Contact: frankyu112058@gmail.com @Description: """ import requests

URL_GET = "httpbin.org/get" URL_POST = "httpbin.org/post" URL_COOKIE = "www.baidu.com/s?tn=020033…"

def Get(): payload = {"lady": "killer", "key": "9"} headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko)\

学习路线:

这个方向初期比较容易入门一些,掌握一些基本技术,拿起各种现成的工具就可以开黑了。不过,要想从脚本小子变成黑客大神,这个方向越往后,需要学习和掌握的东西就会越来越多以下是网络渗透需要学习的内容:
在这里插入图片描述

详情docs.qq.com/doc/DSlhRRFFyU2pVZGhS