Get請求傳參
import requests
url ="https://www.baidu.com/"
# params ={"id":"1001"}
# params ={"id":"1001,1002"}
# params ={"id":[1001,1002]} #不推薦寫法
params ={"id":"1001","name":"1002"}
resp =requests.get(url,params=params)
print(resp.url)
# https://www.baidu.com/?id=1001
# https://www.baidu.com/?id=1001%2C1002 %2C ASCI 值為逗號
# https://www.baidu.com/?id=1001&id=1002
# https://www.baidu.com/?id=1001&name=1002
案例講解
這個案例講的是一個驗證碼+添加cookie的問題
import requests
url_verify_code ='http://192.168.176.128/index.php?m=Home$c=User$a=verify'
r= requests.get(url_verify_code)
r_cookies =r.cookies
# print(r_cookies)
# 設置cookie的值
cookies ={"PHPSEESID":r_cookies["PHPSEESID"]}
url_login ='http://192.168.176.128/index.php?m=Home$c=User$a=do_login'
data ={
"username":"13800001111",
"passwd" :"123456",
"verify_code":"8888"
}
r2= requests.post(url_login,data=data,cookies=cookies)
# 驗證是否登陸成功
print(r2.json())