第15关:Session保持 ⭐⭐⭐

📋 任务目标

学习模拟登录并保持会话状态。

💡 提示

登录账号:student

登录密码:python123

import requests
from bs4 import BeautifulSoup

session = requests.Session()

# 第一步:登录
login_url = 'https://req.haleibc.com/level15'
login_data = {
    'username': 'student',
    'password': 'python123'
}
response = session.post(login_url, data=login_data)

# 第二步:访问需要登录的页面
secret_url = 'https://req.haleibc.com/level15/secret'
response = session.get(secret_url)
soup = BeautifulSoup(response.text, 'html.parser')

# 提取秘密内容
secret = soup.find('div', class_='secret-content')
print(secret.text)

🔐 用户登录

✅ 期望结果

1. 成功登录后能访问秘密页面

2. 未登录时访问秘密页面会被拒绝

3. 使用Session保持登录状态

返回首页 下一关 →