安装图形化支持依赖:
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
代码如下:
import requests
import time
from skimage import io
class spyder:
def __init__(self):
self.login_url = "https://login.test.cn/login"
self.headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
self.session = requests.Session()
self.response = self.session.get(self.login_url,headers=self.headers)
def getVerifyCode(self):
timestamp = int(round(time.time() * 1000))
verifyCode_url = "https://login.test.cn/images/captcha?data=" + str(timestamp)
print(verifyCode_url)
image = io.imread(verifyCode_url)
io.imshow(image)
io.show()
def login(self):
index_url="https://login.test.cn/index"
login_headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Content-Length': '51',
'Connection': 'close'
}
verifyCode = input('请输入验证码')
datas = {
'username': 'test',
'password': 'test1234',
'verifycode': verifyCode
}
print(datas)
result = requests.post(self.login_url,headers=login_headers,data=datas)
print(result.text)
print(result.cookies.get_dict())
if __name__ == '__main__':
toDo = spyder()
toDo.getVerifyCode()
toDo.login()