Flask获取post,get参数,以及 爬虫 requests的get,post参数详解
1.Flask-----get
@app.route('/get',methods=["get"]) def get(): print(f"request.args{request.args}") return 'GET!'
2.Flask-----post
@app.route('/post',methods=["post"]) def post(): print(f"request.form{request.form}") print(f"request.get_data{request.get_data()}") print(f"request.get_json{request.get_json()}") print(f"request.files{request.files}") return 'POST!'
3.爬虫之get方法参数详解
import requests,json url_get = "http://127.0.0.1:5000/get" pm = {"name":"my name is Get"} res = requests.get(url_get,params=pm) print(res.text) ''' 1.params = 字典 request.argsImmutableMultiDict([('name', 'my name is Get')]) '''
4.爬虫之post方法参数详解
import requests,json url_post = "http://127.0.0.1:5000/post" data_data = {"name":"my name is POST_data"} data_json = {"name":"my name is POST_json"} res = requests.post(url_post,data=json.dumps(data_data),json=data_json) print(res.text) """ 有data,那么json就不生效 1.json=data_json request.get_data b'{"name": "my name is POST_json"}' request.get_json {'name': 'my name is POST_json'} 2.json=json.dumps(data_json) request.get_datab'"{\\"name\\": \\"my name is POST_json\\"}"' request.get_json{"name": "my name is POST_json"} 3.data=data_data request.form ImmutableMultiDict([('name', 'my name is POST_data')]) 4.data=json.dumps(data_data) request.get_data b'{"name": "my name is POST_data"}' """
5.爬虫之传参文件:
res = requests.post(url, files={"key": open(file_url, "rb")})
6.我说一个很重要的问题,就是java与python之间接口 交互post请求传参 json的时候一定要在 headers里面加上类型是json才可以(Java很恶心) 'Content-Type': 'application/json', 不服来战 我就觉得恶心,请求其他程序的api也没那么多事
response_data = { "code": 200, "data": return_data, "msg": "success", "status": "200" } print(f"response_data:\n{response_data}") headers = { 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'en-US,en;q=0.8', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Referer': 'http://www.baidu.com/', 'Connection': 'keep-alive', 'Content-Type': 'application/json', } json_response_data = json.dumps((response_data)) print(f"回调的数据:\n{json_response_data}") res = requests.post(notify, data=json_response_data,headers=headers)
-----------------------------------------------------------------------------------------------------------------------------------------

浙公网安备 33010602011771号