'''
上傳文件
files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)`
'''
import requests
#
接口測試的網(wǎng)站, 用戶發(fā)送請求 ,該網(wǎng)站把收到的請求,
# 組成json格式的響應(yīng),返回給用戶
# /post 是post方法
# /get 是get方法
# /delete 是delete方法
url = "http://www.httpbin.org/post"
cs= {
"user" : "admin",
"e-mail": "
[email protected]"
}
path = "e:/test.txt"
with open(path, mode='r') as f:
fs = {
# "file1" 上傳接口的參數(shù)名,是接口定義的
"file1": (path, f),#二元組:(文件名字,文件對象)
"file2": (path, f ,"text/plain") # 三元組(文件名字,文件對象,文件類型)
}
r = requests.post(url, data=cs, files=fs)
print(r.text)
# 租車系統(tǒng),添加車輛時,先上傳車輛的圖片
path = "e:/test.jpg"
url = "http://192.168.2.36:8089/carRental/file/uploadFile.action"
with open(path,mode='rb') as f:
fs = {
"mf" : (path,f ,"image/jpg") # 三元組(文件名字,文件對象,文件類型)
}
r = requests.post(url,data=cs ,files=fs)
print(r.text)
assert "2021-03-18" in r.json()['data']['src']
# 分割線
print("*"* 50)
# 一次上傳多個文件
url="http://www.httpbin.org/post"
path1 = "e:/test.txt"
path2 = "e:/test.jpg"
with open (path1,mode='r') as f1:
with open(path2,mode='rb') as f2:
fs={
"f1":"(path1,f1)",
"f2": (path2,f2,"image/jpg")
}
r = requests.post(url, files=fs)
print(r.text)
'''
設(shè)置cookie
'''
import requests
# 沒有登錄的情況下,訪問該接口,會跳轉(zhuǎn)到登錄頁面
url = "https://www.bagevent.com/account/dashboard"
r = requests.get(url)
print(r.text)
# 使用界面登錄后,服務(wù)器返回的cookie信息,訪問該接口,返回登錄后的統(tǒng)計頁面
hds = {
"Cookie": '_ga=GA1.2.898728043.1615965992; _gid=GA1.2.872824269.1615965992; Hm_lvt_1fc37bec18db735c69ebe77d923b3ab9=1615965992; __auc=13ad1c891783f1445b1a072ed33; sdktoken=68edd18e335220d175f0bf64b259c6f5; uid=ef965a462d80fd4d62c3d118fe742e20; nickName=undefined; avatar=undefined; MEIQIA_TRACK_ID=1psK7g7yqqODa45WOXfKCti4C9P; BAGSESSIONID=02afdf9b-261c-4436-b259-4ddd02e0ca52; JSESSIONID=79B179498FBB86DE1EDDBB623F22A614; __asc=c76a6638178441def3c8c64462f; MEIQIA_VISIT_ID=1pv512xOjWnzNuo9ihmWU9LF9Pa; Hm_lpvt_1fc37bec18db735c69ebe77d923b3ab9=1616050526; _gat=1; BAG_EVENT_TOKEN_=02de735f68204d51009e7edda78e58c13a3fcdd1; BAG_EVENT_CK_KEY_="
[email protected]"; BAG_EVENT_CK_TOKEN_=2440f5d17af838308ba4b390db81af38'
}
r = requests.get(url,headers=hds)
print(r.text)
'''
自動管理cookie的機(jī)制,創(chuàng)建一個session = requests.session(), 自動維護(hù)cookie信息
'''
import requests
s = requests.session() # 創(chuàng)建session
print("登錄之前的cookie信息:",s.cookies)
#百格登錄的接口
url = "https://www.bagevent.com/user/login"
cs = {
"access_type": 1,
"loginType": 1,
"emailLoginWay": 0,
"account": "
[email protected] ",
"password": "qq2780487875",
"remindmeBox": "on",
"remindme": 1}
r = s.post(url, data=cs) #用session 發(fā)送請求
assert "<title>百格活動 - 賬戶總覽</title>" in r.text
# 分割線
print("*"* 50)
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
print("登錄之后的cookie信息:",s.cookies)
# 百格查詢的接口
url = "https://www.bagevent.com/account/dashboard"
r = s.get(url)
# print(r.text)
assert "<title>百格活動 - 賬戶總覽</title>" in r.text
# 退出登錄
url = "https://www.bagevent.com/user/login_out "
r = s.get(url)
print("退出登錄之后的cookie的信息:", s.cookies)
# RequestsCookieJar 轉(zhuǎn)成字典
ts = requests.utils.dict_from_cookiejar(s.cookies)
print(ts)
for k, v in ts.items():# 遍歷字典
print(k, ":", v)
tianGan = ["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"]
diZhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
for i in range(10):
print(tianGan [i%10] +diZhi[i%12],end=" ")
if i !=0 and i%10 ==9:
print()
# for j in range(12):
# print(tianGan
+diZhi[j])