Python.Requests库的基本使用
Requests安装
使用pip安装命令:
pip install requests
打开cmd,输入python然后导入requests如果安装成功没有任何提示
如果提示如下则说明安装失败
ImportError: No module named 'requests'
Requests 基础应用
发送不同类型HTTP请求
requests库内置了不同的方法来发送不同类型的http请求,用法如下所示:
import requests base_url = "http://httpbin.org" # 发生GET类型请求 r_get = requests.get(base_url + "/get") print(r_get.status_code) # 发生POST类型请求 r_post = requests.post(base_url + "/post") print(r_post.status_code) # 发生PUT类型请求 r_put = requests.put(base_url + "/put") print(r_put.status_code) # 发生DELETE类型请求 r_delete = requests.delete(base_url + "/delete") print(r_delete.status_code)
执行结果,200是状态码表示发送请求成功
200 200 200 200
参数传递
传递URL参数
一般在GET请i去中使用字符串(query string)来进行参数传递,在requests库中使用方法如下:
param_data = {'hero': 'leesin'} r_data = requests.get(base_url + '/get', params=param_data) print(r_data.url) print(r_data.status_code)
执行结果
http://httpbin.org/get?hero=leesin
200
传递body参数
在post请求中,一般参数都在请求体(Request body)中传递,在Requests中用法如下:
form_data = {'hero': 'leesin'} r_body = requests.post(base_url + '/post', data=form_data) print(r_body.text)
执行结果
{ "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.22.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" }
请求头定制
如果你想为请求添加HTTP头部,只要简单传递一个dict给headers参数就可以了。
用法如下:
form_data = {'hero': 'leesin'} header = {'user-agent': 'Mozilla/5.0'} r_headers = requests.post(base_url + '/post', data=form_data, headers=header) print(r_headers.text)
返回值
{ "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" }
响应内容
当请求发送成功之后,我们可以获取响应内容。如响应状态码,响应头信息,响应体内容
form_data = {'hero': 'leesin'} header = {'user-agent': 'Mozilla/5.0'} r_response = requests.post(base_url + '/post', data=form_data, headers=header) # 获取响应状态码 print(r_response.status_code) # 获取响应头信息 print(r_response.headers) # 获取响应内容 print(r_response.text) # 将响应内容以json格式返回 print(r_response.json())
返回值
200 {'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': '*', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Date': 'Fri, 28 Jun 2019 14:38:09 GMT', 'Referrer-Policy': 'no-referrer-when-downgrade', 'Server': 'nginx', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '258', 'Connection': 'keep-alive'} { "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" } {'args': {}, 'data': '', 'files': {}, 'form': {'hero': 'leesin'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '11', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'Mozilla/5.0'}, 'json': None, 'origin': '61.144.173.21, 61.144.173.21', 'url': 'https://httpbin.org/post'}