0

好的,所以我在我正在开发的一些应用程序上的 Web 浏览器控件上遇到了很多麻烦。他们都有相同的问题。我想让应用程序浏览网页并读取页面源中的文本并将其写入变量。之后我还需要能够保存文件。

一些源代码:

Public Class Form4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
    MyFolderBrowser.Description = "Select the Folder"
    MyFolderBrowser.ShowNewFolderButton = False
    Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()
    If dlgResult = Windows.Forms.DialogResult.OK Then
        TextBox1.Text = MyFolderBrowser.SelectedPath
    End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox1.Text = "" Then
        MessageBox.Show("You have to select a directory!")
    Else
        WebBrowser1.Navigate("www.realmofthemadgod.com/version.txt")
        System.Threading.Thread.Sleep(3000)
        Dim PageSource As String = WebBrowser1.Document.Body.InnerText
        WebBrowser1.Navigate("http://www.realmofthemadgod.com/AssembleeGameClient" & PageSource & ".swf")
    End If
End Sub

结束类

我遇到的第一件事是它从不等待网页加载,然后再拉出文档文本。我尝试了许多不同的方法来从人们发布的不同解决方案中解决这个问题。奇怪的是,如果我第二次这样做,它似乎总是有效。

如果单击 Button2,我想将最终生成的网页作为 swf 保存到所选目录。

感谢您提供的任何帮助,我一直在到处寻找这个

4

1 回答 1

1

欢迎来到网络抓取的黑暗艺术。首先,我建议使用WebClient而不是 WebBrowser,因为它具有从网站下载数据的离散方法。看起来您的 version.txt 仅包含您想要的数据(并且没有多余的 html),因此我们可以直接下载它。如果您需要解析 html,我会使用HtmlAgilityPack。未经测试的代码可以帮助您入门:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox1.Text = "" Then
        MessageBox.Show("You have to select a directory!")
    Else
        Using wc as New WebClient()
          Dim version = wc.DownloadString("www.realmofthemadgod.com/version.txt")
          Dim swf = "http://www.realmofthemadgod.com/AssembleeGameClient" + version + ".swf"
          wc.DownloadFile(swf,"c:\temp\myswf.swf")
        End Using
    End If
End Sub
于 2013-05-07T23:58:39.803 回答