【问题标题】:I have this code that is currently using web browser. How can I convert this to webclient?我有这段代码当前正在使用网络浏览器。如何将其转换为 webclient?
【发布时间】:2012-01-08 13:59:00
【问题描述】:
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 的方法。谢谢

【问题讨论】:

  • 您已经为WebBrowser 发布了一个事件处理程序——不知道该控件还做了什么,因此无法判断。

标签: vb.net webclient


【解决方案1】:
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

【讨论】:

  • 很酷,感谢 Fardjad ..我会玩一段时间来让它工作,但它给了我很多错误需要解决。但没有什么是我无法完成的。
  • @ChrisWheelous 老实说,这段代码没有经过测试(但我确信正则表达式部分是正确的:D)。我是C# 的人,上面的代码中可能会发现很多错误。如果您成功了,只需编辑答案,我将接受编辑(或将代码粘贴到 pastebin.com 并分享链接,以便我可以使答案正确。)
  • 感谢之前的帮助。我将继续筛选代码,但如果你在 C# 中有它,那么你也可以粘贴工作代码,我也可以使用它。但这里是 pastebin 链接pastebin.com/Q8Zf8Fav
  • @ChrisWheelous 我可以看到粘贴 bin.com 中的代码和我的代码之间没有区别,所以它是工作版本?
  • 抱歉...我误读了答案... .不用担心..我会在这里工作并让它工作..然后我会在这里发布工作代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
相关资源
最近更新 更多