环境搭建
python 安装:建议使用python3.7
pycharm安装
requests安装 :pip3 install requests
requests 基本使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
usage:
>>> import requests
>>> r = requests.get( 'https://www.python.org' )
>>> r.status_code
200
>>> 'Python is a programming language' in r.content
True
... or POST:
>>> payload = dict(key1= 'value1' , key2= 'value2' )
>>> r = requests.post( 'https://httpbin.org/post' , data=payload)
>>> print(r.text)
{
...
"form" : {
"key2" : "value2" ,
"key1" : "value1"
},
...
}
|
获取返回数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
res = requests.post(url,data).text 返回的是text格式
res = requests.post(url,data).json 返回的是json格式
requests上传文件的例子:
import requests
import json
#上传文件
#url = 'https://www.imooc.com/user/postpic'
download_url = 'http://file.mukewang.com/imoocweb/webroot/mobile/imooc7.2.010102001android.apk'
file = {
"fileField":("test.jpg",open("E:/ytxu/test.jpg","rb"),"image/jpg"),
"type":"1"
}
cookie = {
"apsid":"I5ZTVmZmUzMGE1NDY2OTljZjFjYzkyMTMyMjk3MmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANzIxMzU2MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABNdXNoaXNoaV94dUAxNjMuY29tAAAAAAAAAAAAAAAAADVjZDY5ZWYxMGQ2MmFlZDVmNTJkYWQ0ZWNhNjU5MjZhz%2BMFXc%2FjBV0%3DZW"
}
res = requests.get(download_url)
with open("mukewang.apk","wb") as f:
f.write(res.content)
#res = requests.post(url,files=file,cookies=cookie,verify=False).json()
print(res)
#res = requests.post(url,files=file,cookies=cookie,verify=False).json()
print(res)
|
requests 下载apk文件
requests 中 hearder介绍
requests hearder加密
flask环境搭建及简单开发
接口开发:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#coding=utf-8
import requests
import json
from flask import Flask
app = Flask(__name__)
@app .route( "/" )
def logine():
data = json.dump(
{
'username' : "aa" ,
'password' : "111111"
}
)
return data
if __name__ = = "__main__" :
app.run()
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/LinxiHuang/p/13191086.html