python自动化登录获取验证码的代码
时间: 2023-04-01 19:01:11 浏览: 81
以下是 Python 自动化登录获取验证码的代码:
```python
import requests
from PIL import Image
from io import BytesIO
# 登录页面 URL
login_url = 'https://example.com/login'
# 获取验证码图片 URL
captcha_url = 'https://example.com/captcha'
# 创建会话
session = requests.Session()
# 获取登录页面
response = session.get(login_url)
# 获取验证码图片
response = session.get(captcha_url)
captcha_image = Image.open(BytesIO(response.content))
captcha_image.show()
# 输入验证码
captcha_code = input('请输入验证码:')
# 提交登录请求
data = {
'username': 'your_username',
'password': 'your_password',
'captcha': captcha_code,
}
response = session.post(login_url, data=data)
# 检查登录是否成功
if response.status_code == 200 and '登录成功' in response.text:
print('登录成功')
else:
print('登录失败')
```
希望能对你有所帮助。