使用 Session 会话方法自动管理登录状态|臭大佬

臭大佬 2020-03-06 17:46:53 2024
Python 
简介 使用 Session 会话方法自动管理登录状态

代码

# coding:utf-8
import requests

# 实例一个session会话
s =requests.Session()
urlPost = 'http://www.lanrenmb.com/User/index.php/Public/checkLogin.html'
# 设置post数据
dataPost = {
    'username':'00050',
    'password':'lanren00050'
}
# 使用post登录
s.post(urlPost,data=dataPost)
# 保持了登录后的 Cookie 参数设置,我们就可以执行登录后的操作了

downUrl = 'http://www.lanrenmb.com/User/index.php/User/downLoad.html?type=downLoad&aid=7230&url=http%3A%2F%2Fwww.lanrenmb.com%2Fshenghuofuwu%2Ffangchan%2F7230.html'
# 请求下载链接
wdata = s.get(downUrl).content
# 新建文件
with open('file/text.zip','wb') as zip:
    # 将http响应写入文件
    zip.write(wdata)
    print('ok,session test')