将网页内容写入文本文件

时间:2017-03-01 17:14:15

标签: vb.net save text-files

我习惯了VBA,但我从未尝试过VB .NET,我需要将2个网页的文本转换为文本文件。这是我正在使用的代码,但我遇到了问题!我需要做些什么改变才能使它发挥作用?

Public mIE As Object
Public arrText(1) As String
Public Const myFile As String = "C:\myTextFile.txt"

Public Sub Main()
    Dim arrURL(1) As String
    Dim i As Byte

    On Error Resume Next
    Kill (myFile)

    ' Define URL
    arrURL(0) = "http://URL1"
    arrURL(1) = "http://URL2"

    For i = 0 To 1
        'Spawn Internet Explorer
        mIE = CreateObject("InternetExplorer.Application")

        arrText(i) = openWebPage(arrURL(i))

        mIE.Quit()
        mIE.Close()
        mIE = Nothing
    Next

    Call saveToTextFile
End Sub

Public Function openWebPage(myURL As String) As String
    With mIE
        .Top = 0
        .Left = 0
        .Height = 800
        .Height = 600
        .AddressBar = 0
        .StatusBar = 0
        .Toolbar = 0
        .Visible = True
        .navigate (myURL)
    End With

    openWebPage = mIE.document.body.innerText
End Function

Public Sub saveToTextFile()
    Dim oWriter As New System.IO.StreamWriter(myFile )
    Dim i As Byte

    For i = 0 To UBound(arrText)
        oWriter.WriteLine (arrText(i))
    Next

    oWriter.Close()
End Sub

此致 埃利奥·费尔南德斯

1 个答案:

答案 0 :(得分:0)

我认为你可能有找不到的论据,比如fichSGA'。它们可能是您未提供的代码。您可以添加“断点”'在Visual Studio中,然后通过在标准键盘上按F10来踩它们。然后通常您可以将鼠标悬停在变量或项目上,并显示其值。如果它不在那里可能是问题的一部分。这是Vb.NET中一个超级简单的控制台应用程序,用于使用StreamWriter打印文件。在此示例中没有错误捕获,因此请确保您已存在C:\ Test文件夹。

Imports System.IO

 Public Sub WriteTextToFile(text As String, location As String)
    Using sw = New StreamWriter(location)
      sw.Write(text)
    End Using
  End Sub

  Sub Main()
    Dim someText = "I am just some text"
    Dim someMoreText = "I am more text"

    Dim combined = someText + Environment.NewLine + someMoreText

    WriteTextToFile(combined, "C:\Test\Test.txt")
  End Sub