【问题标题】:Writing data to a text file in a format以某种格式将数据写入文本文件
【发布时间】:2012-02-07 06:07:11
【问题描述】:

我正在尝试从网站获取一些特定内容并将其放入文本文件中。我使用了一个列表框来循环我想要处理的 url,另一个来查看数据的输出。现在我想要文本文件中的所有数据,每个项目用“~”sysmbol 分隔。

我在 my.txt 文件中使用的示例链接:http://www.maxpreps.com/high-schools/abbeville-yellowjackets-(abbeville,al)/basketball/previous_seasons.htm

文本文件中的预期数据:
Abbeville 高中篮球统计数据 ~ 球队:11-12 校队 ~ 颜色:栗色、灰色、白色 ....

Imports System.IO.StreamReader
Imports System.Text.RegularExpressions
Imports System.IO


Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim abc As String = My.Computer.FileSystem.ReadAllText("C:\Documents and Settings\Santosh\Desktop\my.txt")
        Dim pqr As String() = abc.Split(vbNewLine)
        ListBox2.Items.AddRange(pqr)

    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        For Each item In ListBox2.Items
            Dim request As System.Net.HttpWebRequest = System.Net.WebRequest.Create(item)
            Dim response As System.Net.HttpWebResponse = request.GetResponse

            Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
            Dim rsssource As String = sr.ReadToEnd
            Dim r As New System.Text.RegularExpressions.Regex("<h1 id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Header"">.*</h1>")
            Dim r1 As New System.Text.RegularExpressions.Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Mascot"">.*</span>")
            Dim r3 As New System.Text.RegularExpressions.Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Colors"">.*</span>")
            Dim r4 As New System.Text.RegularExpressions.Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_GenderType"">.*</span>")
            Dim r5 As New System.Text.RegularExpressions.Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_AthleteDirectorGenericControl"">.*</span>")
            Dim r6 As New System.Text.RegularExpressions.Regex("<address>.*</address>")
            Dim r7 As New System.Text.RegularExpressions.Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Phone"">.*</span>")
            Dim r8 As New System.Text.RegularExpressions.Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Fax"">.*</span>")

            Dim matches As MatchCollection = r.Matches(rsssource)
            Dim matches1 As MatchCollection = r1.Matches(rsssource)
            Dim matches3 As MatchCollection = r3.Matches(rsssource)
            Dim matches4 As MatchCollection = r4.Matches(rsssource)
            Dim matches5 As MatchCollection = r5.Matches(rsssource)
            Dim matches6 As MatchCollection = r6.Matches(rsssource)
            Dim matches7 As MatchCollection = r7.Matches(rsssource)
            Dim matches8 As MatchCollection = r8.Matches(rsssource)


            For Each itemcode As Match In matches
                Dim W As New IO.StreamWriter("C:\" & FileName.Text & ".txt")
                W.Write(itemcode.Value.Split("""").GetValue(2))
                W.Close()

                'ListBox1.Items.Add(itemcode.Value.Split("""").GetValue(2))
            Next
            For Each itemcode As Match In matches1
                ListBox1.Items.Add(itemcode.Value.Split("""").GetValue(2))
            Next
        Next item

    End Sub
End Class

【问题讨论】:

    标签: vb.net visual-studio-2010 text


    【解决方案1】:

    只需像这样将其附加到 Write 语句的末尾即可。

    W.Write(itemcode.Value.Split("""").GetValue(2) & " ~ ")
    

    【讨论】:

    • 在上面的代码中每个循环仅用于匹配,我想要像 Match~match1~match2~match4 这样的文本......
    猜你喜欢
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 2015-04-28
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多