10_27_requests模块
1、get请求:
看源码
import requests url= "https://www.baidu.com" res = requests.get(url) print(res) #响应头、响应状态码、响应报文 print(res.headers) print(res.status_code) print(res.text) #html json xml 返回数据的格式有三种,一般;
2、post
import requests url= " " data = {"手机号":"13000000000"} res = requests.post(url,data) print(res.headers) print(res.status_code)
print(res.text)
#和 res.json(),响应正文的格式不同,
# 如果是 html、xml、json -->可以是 text
#只有json类型的返回值才支持json
3、cookies
res.cookies 类字典形式,可以 res.cookies[key]
4、模拟浏览器 在headers 中加入 uer_agent
5、待续 分装。。
import requests class HttpRequest: def http_requests(self,url,data,method,cookie=None): if method =='get': res = requests.get(url, data, cookies=cookie) else: res = requests.post(url, data, cookies=cookie) return res if __name__ == '__main__': url_login = 'http://119.23.241.154:8080/futureloan/mvc/api/member/login' url_recharge = 'http://119.23.241.154:8080/futureloan/mvc/api/member/recharge' login_data = {"mobilephone": "18688773467", "pwd": "123456"} recharge_data = {"mobilephone": "18688773467", "amount": "1000"} #登录 res = HttpRequest().http_requests(url_login,login_data,'post') print("登录结果是:",res.json()) #充值 recharge_res = HttpRequest().http_requests(url_recharge,recharge_data,'get',res.cookies) print("充值结果是:",recharge_res.json())