自动化接口requests操作

import requests
# 将接口参数直接写在url中
url = 'http://apis.juhe.cn/simpleWeather/query?city=西安&key=437cd60d707de5cf8037ef8da861fb0f'
response = requests.get(url)# 发起get请求,将响应的对象赋值给变量response。
t = response.text # 获取接口返回的正文
print(t)
print(type(t))
j = response.json() # 接收接口返回的json格式数据,并且将json转换为字典
print(j)
print(type(j))
h = response.headers # 获取响应头
print(h)
print(type(h))
c = response.cookies # 获取响应的cookie
print(c)
print(type(c))
c_new = dict(c)
print(c_new)
s = response.status_code # 获取响应的状态码
print(s)
r = response.raw # 获取原始响应
print(r)
o = response.ok # 获取接口是否访问成功的bool型值
print(o)
reason = response.reason # 获取接口访问失败的原因
print(reason)
rai = response.raise_for_status() #获取接口请求失败时抛出的异常
print(rai)

url = 'http://apis.juhe.cn/simpleWeather/query'
param = {"city":"西安","key":"437cd60d707de5cf8037ef8da861fb0f"} # 不把参数写在url中,参数组成为字典
# param = 'city=西安&key=437cd60d707de5cf8037ef8da861fb0f' #form格式传参
response2 = requests.get(url,params=param)
j = response2.json()
print(j)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值