python中的requests模块
import requests #post请求============================ # url='http://127.0.0.1:8000/api/update'#接口入参为str # data = {'username':'123','password':'345'} # r = requests.post(url,data) # print(r.url)#get请求返回url #http://127.0.0.1:8000/api/update?username=123&password=345 # print((r.json()))#返回字典 # # url_json='http://127.0.0.1:8000/api/check'#接口入参为json # data = {'username':'123','password':'345'} # r_json = requests.post(url_json,json=data)#json=data表示入参是json # print((r_json.json()))#返回字典 #get请求================================ # url = 'http://xx.xx.xx.xx:8080/xx/user/login' # headers = {'Cookie':'JSESSIONID=7C8052371BF7BFDA5865E895E6E49BEC; UAPSESSIONID=D1D0E1D83C344CCCB221712F79D4BBD4; _jfinal_captcha=aa9b345535f94af09886898ec1a2424a', # 'Content-Type':'text/html;charset=UTF-8'} # r = requests.get(url) # if r.status_code==200: # print('访问正常') # else: # r.raise_for_status() # #下载=================== # url_p ='https://pics5.baidu.com/feed/d833c895d143ad4bb2226779dcd84da5a50f065a.jpeg?' # r = requests.get(url_p) # with open('a.jpg','wb') as f: # f.write(r.content) # #上传=========================== # url = 'http://xxxxxx/api/file/file_upload' # file = {'file':open('a.jpg','rb')} # r = requests.post(url,files=file) # if r.status_code==200: # print('成功') # else: # r.raise_for_status()#非200 抛出错误异常 # r.encoding 获取网页编码格式 # r.text 获取响应类型为str格式 常见get请求获取网页数据 # r.content 以字节的方式去显示,中文显示为字符 常用文件/图片类型 # r.headers 获取响应头 # r.status_code 获取响应状态码 # r.cookies 获取cookies # r.url 获取请求的url # r.json() 获取响应为json的数据,前提是响应报文一定是json否则报错