【问题标题】:How to get a form captcha image with Jsoup?如何使用 Jsoup 获取表单验证码图像?
【发布时间】:2016-04-01 18:28:37
【问题描述】:

我正在为我的学校网站开发一个应用程序,我正在使用 jsoup 来解析 html。

我遇到了验证码图片问题,我看到了this 问题并且我已经实施了,但我没有得到与网站上显示的相同的图片。

我怎样才能获得相同的图像验证码,该网站正在使用 BotDetectCaptcha 我有点困惑如何在我的网站上专门进行此操作

School Website

【问题讨论】:

  • 你可能需要cookies。
  • 我正在使用这段代码,我得到了 cookie,但这对我有什么帮助? Connection.Response response = Jsoup.connect(url) .timeout(300000) .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0") .method(Connection.Method.GET).execute(); cookies = response.cookies();
  • @JonathanAxel 我认为每次加载验证码时都会更改验证码的理念:)

标签: java android jsoup captcha


【解决方案1】:

如 SLaks 评论中所述,您可能缺少一些 cookie。

这是一个带有所提供 url 的工作示例:

// Load the initial page for getting the required cookies
Connection conn = Jsoup.connect("https://www.saes.upiicsa.ipn.mx/");
Document d = conn.get();

Element captcha = d.select("#c_default_ctl00_leftcolumn_loginuser_logincaptcha_CaptchaImage").first();
if (captcha == null) {
    throw new RuntimeException("Unable to find captcha...");
}

// Fetch the captcha image
Connection.Response response = Jsoup //
        .connect(captcha.absUrl("src")) // Extract image absolute URL
        .cookies(conn.response().cookies()) // Grab cookies
        .ignoreContentType(true) // Needed for fetching image
        .execute();

// Load image from Jsoup response
ImageIcon image = new ImageIcon(ImageIO.read(new ByteArrayInputStream(response.bodyAsBytes())));

// Show image
JOptionPane.showMessageDialog(null, image, "Captcha image", JOptionPane.PLAIN_MESSAGE);

输出

在 JSoup 1.8.3 上测试

【讨论】:

    【解决方案2】:

    您说您得到的图片与您在网站上看到的图片不同... 这很正常,因为每次刷新页面时图像都不一样。

    【讨论】:

    • 是的,但是当我在 android 上执行该方法时,我会得到一个带有 4 个元素的图像,如果你可以在网站上看到超时,你刷新它是一个 3 个元素的图像,我不不知道为什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2011-08-03
    • 2017-02-05
    • 1970-01-01
    相关资源
    最近更新 更多