网页的验证码

1.首先可以写一个产生随机验证码的aspx文件,如下产生四位数字:
    private void Page_Load(object sender, System.EventArgs e)
    {
        this.CreateCheckCodeImage(GenerateCheckCode());
    }

    private string GenerateCheckCode()
    {
        int number;
        char code;
        string checkCode = String.Empty;

        System.Random random = new Random();

        for (int i = 0; i < 4; i++)
        {
            number = random.Next();

            code = (char)('0' + (char)(number % 10));
            checkCode += code.ToString();
        }

        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
        //为了今后的验证

        return checkCode;
    }

    private void CreateCheckCodeImage(string checkCode)
    {
        if (checkCode == null || checkCode.Trim() == String.Empty)
            return;

        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
        Graphics g = Graphics.FromImage(image);

        try
        {
            //生成随机生成器
            Random random = new Random();

            //清空图片背景色
            g.Clear(Color.White);

            //画图片的背景噪音线
            for (int i = 0; i < 15; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(checkCode, font, brush, 2, 2);

            //画图片的前景噪音点
            for (int i = 0; i < 80; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }
这个文件名设为CheckCode,它放在CheckCode.aspx的CheckCode.aspx.cs下就可。
2.然后在需要放验证图片的地方加上如下代码:
<img src="common/CheckCode.aspx" alt=""/>就能产生验证码了
3.当需要取得验证图片值的时候可参考编写如下代码
if (String.Compare(Request.Cookies["CheckCode"].Value, validateTb.Text, true) != 0)
        {
            //validateTb为文本框
            validateTb.Focus();
            ErrorMsg.Text = "验证码输入错误!";
            return;
        }

 

TraceBack:http://community.csdn.net/Expert/topic/5739/5739366.xml?temp=1.18655E-03

转载于:https://www.cnblogs.com/hdjjun/archive/2008/06/17/1223797.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Selenium识别网页验证码的步骤: 1. 首先,你需要安装Selenium库。你可以使用以下命令来安装: ```shell pip install selenium ``` 2. 导入Selenium库和其他必要的库: ```python from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC ``` 3. 创建一个WebDriver对象,选择一个浏览器驱动程序(例如Chrome驱动程序): ```python driver = webdriver.Chrome('path_to_chrome_driver') ``` 4. 打开目标网页: ```python driver.get('https://example.com') ``` 5. 使用Selenium定位到验证码图片元素,并获取验证码图片的URL: ```python captcha_image = driver.find_element_by_id('captcha-image') captcha_image_url = captcha_image.get_attribute('src') ``` 6. 下载验证码图片: ```python import requests response = requests.get(captcha_image_url) with open('captcha.png', 'wb') as f: f.write(response.content) ``` 7. 使用第三方库(例如Pillow)加载并处理验证码图片: ```python from PIL import Image captcha_image = Image.open('captcha.png') # 进行验证码图片的处理,例如裁剪、灰度化、二值化等 ``` 8. 使用第三方库(例如Tesseract)对处理后的验证码图片进行识别: ```python import pytesseract captcha_solution = pytesseract.image_to_string(captcha_image) ``` 9. 使用Selenium定位到验证码输入框,并填入解析出的验证码: ```python input_box = driver.find_element_by_id('captcha-input-box-id') input_box.send_keys(captcha_solution) ``` 10. 最后,你可以继续进行其他操作,例如提交表单或点击按钮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值