2012-01-08 36 views
0
Private Sub WebBrowser1_DocumentCompleted_1(ByVal sender As System.Object, ByVal e As  System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 
For Each Image As HtmlElement In WebBrowser1.Document.Images 
    If Image.GetAttribute("src").Contains("captcha") Then 
     Dim Web As New Net.WebClient 
     PictureBox1.Image = New Drawing.Bitmap(New IO.MemoryStream(Web.DownloadData(Image.GetAttribute("src")))) 
    End If 
Next 
End Sub 
End Class 

我試圖找到一種方法將此轉換爲webclient。由於我有這個代碼,目前正在使用網絡瀏覽器。我怎樣才能將其轉換爲webclient?

+0

您已經發布了一個'WebBrowser'的事件處理程序 - 不知道還有什麼與控制完成的,它是不可能告訴。 – Oded 2012-01-08 14:16:00

回答

0
Dim webClient As New System.Net.WebClient 

Dim url As String = "yourPageURL" 
Dim htmlByte() As Byte = webClient.DownloadData(url) 

Dim htmlString As String 
htmlString = System.Text.Encoding.UTF8.GetString(htmlByte) 

' Generally using Regexes for Parsing HTML is not a good idea, this is just an example, you would use something like HTMLAgilityPack for parsing. 

' A quick and dirty hack that just make it work when html is minified 
htmlString = htmlString.Replace("<", System.Environment.NewLine + "<"); 

Dim matches As System.Text.RegularExpressions.MatchCollection 
Dim pattern As String = "<img.*?src\s*=\s*[""'](?<src>[^""']+)[""'].*?>" 
matches = System.Text.RegularExpressions.Regex.Matches(htmlString, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase) 

For Each match As System.Text.RegularExpressions.Match In matches 
    If (Not match.Groups.Item("src").ToString().ToLower().Contains("captcha")) Then Continue For 

    PictureBox1.Image = New Drawing.Bitmap(New System.IO.MemoryStream(webClient.DownloadData(match.Groups.Item("src").ToString()))) 
Next 
+0

很感謝Fardjad ..我會在這裏玩一會兒才能讓它起作用,但是正確的是它給了我一個解決錯誤的方法。但沒有什麼我不能通過。 – 2012-01-08 14:42:36

+0

@ChrisWheelous說實話,這段代碼沒有經過測試(但我確定Regex部分是正確的:D)。我是一個C#人,在上面的代碼中可能會發現很多錯誤。如果你使它工作,只需編輯答案,我會接受編輯(或將代碼粘貼到pastebin.com並分享鏈接,這樣我就可以使答案正確)。 – fardjad 2012-01-08 14:48:23

+0

感謝以前的幫助。我將繼續對代碼進行篩選,但如果你使用C#,那麼你可以粘貼工作代碼,我也可以使用它。但這裏是pastebin鏈接http://pastebin.com/Q8Zf8Fav – 2012-01-08 14:55:54

相關問題