python 获取验证码的脚本

python Script for getting a captcha

所以我正在为我的应用程序做这个网站抓取。我需要下载验证码图像以将其显示给用户。但是每次我访问验证码 url 它都会生成一个新的验证码。如何下载自动登录的动态生成的验证码

例如:https://academics.vit.ac.in/student/stud_login.asp

这里我使用下面的脚本下载验证码>>>

from bs4 import BeautifulSoup
import urllib2
import urllib

url = "https://academics.vit.ac.in/student/stud_login.asp"
content = urllib2.urlopen(url)
soup = BeautifulSoup(content)
img = soup.find('img',id ='imgCaptcha')
print img
urllib.urlretrieve(img['src'],'captcha.bmp')

但是这个脚本似乎无法正常工作。

1) 一种解决方案是截屏并裁剪验证码。 但是我需要一个不同的解决方案,因为我要在各种屏幕尺寸的设备上工作,所以截屏不能解决问题。

img['src']returns一个亲戚url-captcha.asp。你必须把它变成绝对url才能使用它(https://academics.vit.ac.in/student/captcha.asp)。

import urlparse
urllib.urlretrieve(urlparse.urljoin(url, img['src']), 'captcha.bmp')