我有这个代码,目前正在使用Web浏览器。我怎样才能将其转换为webclient?

时间:2012-01-08 13:59:00

标签: vb.net webclient

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。感谢

1 个答案:

答案 0 :(得分: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