如果 src 使用 java 函数,如何获取验证码图像?

how to get captcha image if src use java function?

如何从 this website 抓取验证码 验证码图片。

我试过 MSHTML,但该网站使用 java 脚本函数在其 src 中显示检索验证码。请尝试回答我如何实现这一点。

Imports MahApps.Metro.Controls
Imports System.Net
Imports System.Windows.Forms


Class MainWindow
    Inherits MetroWindow

    Private Sub MetroWindow_Loaded(sender As Object, e As RoutedEventArgs)
        wb.Navigate("https://www.irctc.co.in/eticketing/loginHome.jsf")
        AddHandler wb.LoadCompleted, AddressOf wb_Loaded
    End Sub
    Private Sub btngo_Click(sender As Object, e As RoutedEventArgs) Handles btngo.Click
        Dim htmldoc As MSHTML.IHTMLDocument2 = wb.Document
        Dim usrtxtdoc As MSHTML.IHTMLElement = htmldoc.all.item("j_username", 0)
        Dim usrpwddoc As MSHTML.IHTMLElement = htmldoc.all.item("j_password", 0)
        Dim captchadoc As MSHTML.IHTMLElement = htmldoc.all.item("j_captcha", 0)
        usrtxtdoc.innerText = txtusrname.Text
        usrpwddoc.innerText = txtpwd.Text
        captchadoc.innerText = txtcaptcha.Text
    End Sub

    Private Sub wb_Loaded(sender As Object, e As System.Windows.Navigation.NavigationEventArgs)
        MsgBox("Loaded")
        Dim htmldoc As MSHTML.IHTMLDocument2 = wb.Document
        Dim htmldoc2 As MSHTML.HTMLDocument = wb.Document
        Dim captchaimg As MSHTML.HTMLImg = htmldoc.all.item("cimage", 0)
        Dim bitmap As New BitmapImage
        bitmap.BeginInit()
        bitmap.UriSource = New Uri(wb.FindResource("captchaImage"))
        bitmap.EndInit()
        imgcaptcha.Source = bitmap
    End Sub

    Private Sub wb_Navigated(sender As Object, e As NavigationEventArgs) Handles wb.Navigated
        lblwbstatus.Content = "Load Completed"
    End Sub

    Private Sub wb_Navigating(sender As Object, e As NavigatingCancelEventArgs) Handles wb.Navigating
        lblwbstatus.Content = "Navigating Please wait"
    End Sub

    Private Sub lblwbstatus_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles lblwbstatus.MouseDoubleClick
        wb.Refresh()
    End Sub
End Class

您可以从 this link

下载源代码

通常你会这样做:

    Dim htmldoc As mshtml.IHTMLDocument2 = wb.Document.DomDocument
    Dim captchaimg As mshtml.HTMLImg = htmldoc.all.item("cimage", 0)
    Dim imgRange As IHTMLControlRange = htmldoc.body.createControlRange()
    For Each img As IHTMLImgElement In htmldoc.images
        If img.nameProp = "captchaImage" Then
            imgRange.add(img)
            imgRange.execCommand("Copy", False, Nothing)
            Using bmp As Bitmap = Clipboard.GetDataObject().GetData(DataFormats.Bitmap)
                bmp.Save("c:\test.bmp")
            End Using
        End If
    Next

但是,由于 Internet Explorer 问题,该图像有一个 alpha 通道未被复制到剪贴板(您可以在此处阅读 Copying image from page results in black image)。

其他方法是检查 Internet Explorer 缓存,但由于 HTTP headers,该图像不会被缓存,所以你运气不好。

考虑到以下方面,上面的代码工作得很好: 1- 项目类型- 如您的网页目标示例 ("IMG") 所示。 2- 图片专有名称示例:CaptchaImg.jpg 写成CaptchaImg.jpg 3- 添加对 (mshtml) 的引用并将 mshtml 导入您的项目 4- 右键单击​​项目上的引用 --> 单击添加引用 --> -->浏览按钮点击-选择或导航到---> C:\Windows\assembly\GAC\Microsoft.mshtml.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll ----> 确定按钮单击 --- 这会将 Microsoft.mshtml.dll 添加到您的参考 最后将导入到您的项目中(导入 mshtml)。 5- 更改目录 bmp.Save("c:\test.bmp")---> 例如 bmp.Save("c:\test\test.bmp") 为安全和管理权限特权。