【问题标题】:Can this is code made more nice and correct of web scraping? [closed]这可以使网页抓取的代码更加美观和正确吗? [关闭]
【发布时间】:2012-01-30 15:17:05
【问题描述】:

我正在尝试获取网站的学校信息,并希望将其保存为 Excel 表格,每列都有详细信息,最初的开始是下面的代码帮助我走得更远。 列标题:我拥有的学校列表的学校名称、吉祥物、地址、类型、电话、传真等。例如我使用了一个链接。

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

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim request As System.Net.HttpWebRequest = System.Net.WebRequest.Create("http://www.maxpreps.com/high-schools/abbeville-yellowjackets-(abbeville,al)/home.htm")
        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
            ListBox1.Items.Add(itemcode.Value.Split("_").GetValue(4))
            ListBox1.Items.Add(itemcode.Value.Split("><").GetValue(1))
        Next
        For Each itemcode As Match In matches1
            ListBox1.Items.Add(itemcode.Value.Split("_").GetValue(4))
            ListBox1.Items.Add(itemcode.Value.Split("><").GetValue(1))

        Next
    End Sub
End Class

【问题讨论】:

    标签: vb.net web web-crawler web-scraping


    【解决方案1】:

    您正在寻找Code Review。无论如何,是的,你可以让它变得更好。首先,您已经导入了 System.Text.RegularExpressions 命名空间。您无需完全限定Regex。接下来,您可以在比赛中使用

    接下来,您可以使用WebClient 代替所有HttpWebRequest 混乱。这是一个开始:

    Imports System.Net
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Using wc As New WebClient()
                rssource = wc.DownloadString("http://www.maxpreps.com/high-schools/abbeville-yellowjackets-(abbeville,al)/home.htm")
            End Using
    
            Dim r  As New Regex("<h1 id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Header"">(.*?)</h1>")
            Dim r1 As New Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Mascot"">(.*?)</span>")
            Dim r3 As New Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Colors"">(.*?)</span>")
            Dim r4 As New Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_GenderType"">(.*?)</span>")
            Dim r5 As New Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_AthleteDirectorGenericControl"">(.*?)</span>")
            Dim r6 As New Regex("<address>(.*)</address>")
            Dim r7 As New Regex("<span id=""ctl00_NavigationWithContentOverRelated_ContentOverRelated_Header_Phone"">(.*?)</span>")
            Dim r8 As New 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
                'ListBox1.Items.Add(itemcode.Value.Split("_").GetValue(4))
                'Use columns or something instead
                ListBox1.Items.Add(itemcode.Groups(1).Value)
            Next
    
            For Each itemcode As Match In matches1
                ListBox1.Items.Add(itemcode.Groups(1).Value)
            Next
        End Sub
    End Class
    

    接下来,考虑给正则表达式起有意义的名称,将它们命名为StaticCompiled 以提高效率,并且根本不使用正则表达式。哦,还有,改用 HTML 解析器。

    【讨论】:

      猜你喜欢
      • 2021-07-10
      • 2021-06-25
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多