码迷,mamicode.com
首页 > 其他好文 > 详细

requests模块详解

时间:2020-06-26 12:57:29      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:files   var   images   pat   info   ext   出现   自动识别   直接   

# 第一步:准备请求的相关数据
# 接口地址
url = "http://api.lemonban.com/futureloan/member/register"
# 第二部, 准备请求的参数
data = {
    "mobile_phone": "18189098765",
    "pwd": "lemonban"
}
# 第三部 添加请求头
headers = {
    "X-Lemonban-Media-Type":"lemonban.v1"
}
# 第四部 发送请求,获得相应内容
# 发送请求
response = requests.post(url=url, json=data,headers=headers)
# 打印返回内容
print(response.text)
# 打印请求头内容
print(response.request.headers)


"""
# 默认的请求头
{‘User-Agent‘: ‘python-requests/2.21.0‘,
 ‘Accept-Encoding‘: ‘gzip, deflate‘,
 ‘Accept‘: ‘*/*‘,
 ‘Connection‘: ‘keep-alive‘, 
 ‘Content-Length‘: ‘50‘,
 ‘Content-Type‘: ‘application/json‘}
 
 # 自己添加过X-Lemonban-Media-Type字段的请求头
 {‘User-Agent‘: ‘python-requests/2.21.0‘, 
 ‘Accept-Encoding‘: ‘gzip, deflate‘, 
 ‘Accept‘: ‘*/*‘, 
 ‘Connection‘: ‘keep-alive‘,
 ‘X-Lemonban-Media-Type‘: ‘lemonban.v1‘,
 ‘Content-Length‘: ‘50‘, 
 ‘Content-Type‘: ‘application/json‘}
 
"""

requests 模块处理各种请求

#       --------------------------post请求----------------------

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

data = {"phone":"15567899876","pwd":"lemonban"}

# 发送请求
# 参数类型为:"Content-Type": "application/json",发送请求的时候,使用json来传递参数
# response = requests.post(url=url,json=data)

# 参数类型为:Content-Type": "application/x-www-form-urlencoded,使用data来进行传递
response = requests.post(url=url,data=data)

# 文件上传怎么传递参数:
# 文件上传接口的参数类型为:"Content-Type": "multipart/form-data;文件应该使用files这个参数来进行传递
# data = {"phone":"15567899876","pwd":"lemonban"}
# file = {
#     "pic": ("bj01.png", open(r"C:\Users\MuSen\Desktop\page\image\ico.png", "rb"), "image/png")
# }
#
# response = requests.post(url=url, files=file,data=data)

# 获取返回数据
print(response.text)
# ----------------------------get请求-----------------------------------
# get请求的参数:查询字符串参数

# 处理方式一:放到url地址后面
# url = "http://httpbin.org/get?name=musen&age=18"

# 处理方式二:使用params来进行传递
# url1 = "http://httpbin.org/get"
# data = {
#     "name":"musen",
#     "age":18
# }
# response = requests.get(url=url1,params=data)
# print(response.text)

requests 请求返回数据提取

# 第一步:准备请求的相关数据
# 接口地址
url = "http://api.lemonban.com/futureloan/member/login"

# 请求的参数
data = {
    "mobile_phone": "18189098765",
    "pwd": "lemonban"
}
# 请求头
headers = {
    "X-Lemonban-Media-Type": "lemonban.v1"
}
# 发送请求
response = requests.post(url=url, json=data, headers=headers)

# 打印返回内容
# text属性:
# res1 = response.text
# print(res1,type(res1))
# # 错误示范
# res2 = eval(res1)
# print(res2,type(res2))

# json方法:将字符串中的json类型数据转换为对应的python值
# 使用json方法的前提:返回数据必须是json格式的
# res3 = response.json()
# print(res3, type(res3))


# content属性
# res5 = response.content.decode("utf8")
# print(res5, type(res5))

关于请求内容获取和文件下载

import requests

# res = requests.get(url="http://www.lemonban.com/")
# 自动识别内容的编码方式,有可能识别不准确出现乱码
# print(res.text)

# 手动自动编码方式,对页面内容进行解码
# print(res.content.decode("utf-8"))


# 文件下载
res = requests.get(url="http://www.lemonban.com/images/upload/image/20190219/1550554131699.jpg")

print(res.content)

with open("lemon.png","wb") as f:
    f.write(res.content)

提取请求返回的数据

import jsonpath

res = {‘code‘: 0,
       ‘msg‘: ‘OK‘,
       ‘data‘: {‘id‘: 7800007,
                ‘leave_amount‘: 0.0,
                ‘mobile_phone‘: ‘18189098765‘,
                ‘reg_name‘: ‘xxx‘,
                ‘reg_time‘: ‘2020-03-21 09:49:27.0‘,
                ‘type‘: 1,
                ‘token_info‘: {‘token_type‘: ‘Bearer‘,
                               ‘expires_in‘: ‘2020-03-21 11:16:59‘,
                               ‘token‘: ‘eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc4MDAwMDcsImV4cCI6MTU4NDc2MDYxOX0.-zjbWEbXF9qdfvW1Wn0640HZnv3Xkdrx0nDedRTcsgk_URgU185yA-e2SjQUvVfsjA-FpJSKSOF4jjB-Jyv47A‘}},
       ‘data1‘: {‘id‘: 7800007,
                 ‘leave_amount‘: 0.0,
                 ‘mobile_phone‘: ‘18189098765‘,
                 ‘reg_name‘: ‘xxx‘,
                 ‘reg_time‘: ‘2020-03-21 09:49:27.0‘,
                 ‘type‘: 1,
                 ‘token_info‘: {‘token_type‘: ‘Bearer‘,
                                ‘expires_in‘: ‘2020-03-21 11:16:59‘,
                                ‘token‘: ‘eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc4MDAwMDcsImV4cCI6MTU4NDc2MDYxOX0.-zjbWEbXF9qdfvW1Wn0640HZnv3Xkdrx0nDedRTcsgk_URgU185yA-e2SjQUvVfsjA-FpJSKSOF4jjB-Jyv47A‘}},
       ‘copyright‘: ‘Copyright xxx © 2017-2019 xxxxx All Rights Reserved‘}

#  使用字典的键值对 提取方式去提取
# # 提取用户的id
# member_id = res["data"]["id"]
# print(member_id)
#
# # 提取token值
# token = res["data"]["token_info"]["token"]
# print(token)


# 使用jsonpath来提取
member_id = jsonpath.jsonpath(res, "$.data..id")[0]
print(member_id,type(member_id))

token = jsonpath.jsonpath(res, "$.data1..token")[0]
print(token)

"""
. 代表直接子节点
.. 代表子孙节点(不管层级)

"""

requests模块详解

标签:files   var   images   pat   info   ext   出现   自动识别   直接   

原文地址:https://www.cnblogs.com/addicated/p/13194407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!