python之验证码生成

33次阅读

共计 625 个字符,预计需要花费 2 分钟才能阅读完成。

今天向大家总结一下 python 在做项目时用到的验证码生成工具:gvcode 与 captcha
gvcode
全称:graphic-verification-code
安装:
pip install gvcode
使用:
import gvcode

s, v = gvcode.generate() #序列解包

s.show() #显示生成的验证码图片

print(v) #打印验证码字符串
效果:

captcha
安装:
pip install captcha
使用:
from captcha.image import ImageCaptcha
from random import randint
list = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’,
‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’,
”, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’]
chars = ”
for i in range(4):
chars += list[randint(0, 62)]
image = ImageCaptcha().generate_image(chars)

image.show()
效果:

正文完
 0