Python requests 模拟登录示例代码

2023-04-17 00:00:00 示例 登录 模拟

import requests

登录页面url

login_url = 'http://www.example.com/login'

需要提交的数据(根据实际情况修改)

data = {
'username': 'pidancode.com',
'password': 'mypassword'
}

创建session对象

session = requests.Session()

发送post请求,模拟登录

response = session.post(login_url, data=data)

登录成功后访问需要登录才能访问的页面(例如个人主页)

profile_url = 'http://www.example.com/profile'
response = session.get(profile_url)

查看返回结果

print(response.text)

执行完毕后关闭session

session.close()

相关文章