65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 ASP.NET 进行数据抓取(Data Scraping)与 WhoIs 搜索

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.69/5 (20投票s)

2004年6月24日

3分钟阅读

viewsIcon

88679

downloadIcon

2070

使用 ASP.NET 进行 WhoIs 搜索的数据抓取

Sample Image - DataScraping.jpg

关于这篇文章

网络上有很多关于 WHOIS 搜索的例子,但我想通过数据抓取技术来解释这个问题。我主要关注的不是 WHOIS 搜索,而是如何在 Web 应用程序中使用数据抓取。

引言

我们经常想知道某个域名的所有者。为了获取注册信息,我们会访问相应的注册机构(DENIC、Network Solutions 等)并启动所谓的 WHOIS 查询(查找),但这并非总是可行,有时您想在 Web 表单中实现此搜索。本文中使用的源代码需要 Web 服务器上安装 Microsoft .NET Framework SDK。编写这篇文章是为了在 ASP.NET 项目中构建 WHOIS 搜索和域名可用性搜索的需求。本文演示了如何使用数据挖掘技术在 ASP.NET 中执行 WHOIS 搜索和域名可用性搜索。

背景

在本文中,为了从远程 Internet 资源中进行数据挖掘/数据抓取,我使用了 System.Net.WebClient 类。这个类提供了用于将数据发送到由 URI 标识的资源以及从该资源接收数据的常用方法,它在内部使用 WebRequest 类来提供对 Internet 资源的访问。在这种情况下,我使用 WebClient 类的 Download 方法,该方法从资源下载数据并返回一个字节数组。以字节形式接收的数据被编码为系统的当前 ANSI 格式。为了解析此处接收的数据,我使用了 Regex 类,它包含静态方法,允许使用其他正则表达式类,而无需显式实例化其他类的对象。最后,我们需要的信息是借助 Match 类提取的,该类表示正则表达式匹配操作的结果。

项目

为了在 ASP.NET 中实现数据挖掘,在这里我将使用 WHOIS(关于特定域名的信息)和域名可用性,通过使用 www.directnic.com 的 WHOIS 服务器来解释。为了方便起见,您可以选择您喜欢的任何其他服务器。您可以在任何地方找到此类服务器的列表,其中一些链接是

让我们从我们的例子开始。当用户输入他想查看其信息的域名时,用户输入的域名会包含在 URL 字符串中以形成查询字符串。

 Dim strURL As String = _
  "http://www.directnic.com/whois/index.php?query=" _
  + txtDomain.Text

为了提取此 URL 字符串的结果,我们需要捕获 directnic 服务器返回的输出。为此,我们使用 WebClient 类的 Download 方法,该方法以字节格式返回数据。

            Dim web As New WebClient()
            ' byte array to store the extracted bufData by webclient

            Dim bufData As Byte()
            bufData = web.DownloadData(strURL)

我们的工作完成了一半,我们获得了数据,现在我们需要将这些字节数据转换为标准格式,以便我们可以轻松进行解析。

    ' got the bufData now convert it into string format

            ' firstLevelbufData is a string variable

            firstLevelbufData = Encoding.Default.GetString(bufData)

最后,我们以标准格式获取数据,该数据存储在 firstLevelbufData 字符串变量中。该变量包含许多不必要的信息,我们需要仅提取所需的信息。因此,我将提取两个匹配的标签或字符串中存在的信息。为此,我使用了两个变量,firstlast。您可以根据您的要求更改 firstlast 变量的值,但为此,您必须了解您正在接收的数据的格式。

    Dim first, last As String
            ' chr(34) is used for (") symbol

            ' <p class="text12" > this is the first string

            first = "' <p class=" + Chr(34) + "text12" + Chr(34) + ">"
            last = "' </p>"

从 HTML 格式的数据中提取信息的最佳方法是使用正则表达式解析。在这里,我使用 firstlast 变量并借助 System.Text.RegularExpressions 命名空间中可用的 Regex 类来创建一个正则表达式。

 Dim RE As New Regex(first + _
  "(?<MYDATA>.*?(?=" + last + "))", _
  RegexOptions.IgnoreCase Or RegexOptions.Singleline)

剩下的主要任务是提取与上述正则表达式匹配的所需数据。为此,我们需要 Match 类,它表示正则表达式匹配操作的结果。

     txtResult.Text = m.Groups("MYDATA").Value + ""

清单 1:HTML 代码 (Whois.aspx)

 <HTML>
    <HEAD>
        
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
        <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="358" border="0">
            <TR>
            <TD>
                <asp:Label id="Label2" Runat="server">www.</asp:Label>
                <asp:TextBox id="txtDomain" Runat="server">
                Check Domain</asp:TextBox></TD>
            <TD>
                <asp:Button id="btnQuery" Text="Check Domain" Runat="server">
                </asp:Button></TD>
        </TR>
        <TR>
            <TD>
                <asp:Label id="txtResult" Runat="server"></asp:Label></TD>
            <TD></TD>
        </TR>
        </TABLE>
        </form>
    </body>
</HTML>

清单 2:服务器端代码 (WhoIs.aspx.vb)

Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Collections
Imports System.Net
Imports System.Text.RegularExpressions

     Public Class WhoIs
    Inherits System.Web.UI.Page
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents btnQuery As System.Web.UI.WebControls.Button
    Protected WithEvents txtResult As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents txtDomain As System.Web.UI.WebControls.TextBox


    Private Sub Page_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
        ' Adds the java script code for clearing  the existing text

        ' from the text box when user wants to

        ' enter a new domain name

        txtDomain.Attributes.Add("onclick", "this.value='';")
    End Sub

    Private Sub btnQuery_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnQuery.Click
        ' Stores the bufData extracted from the webclient 

        Dim firstLevelbufData As String 
        Try
            ' similarly we can select any server address for bufData mining

            Dim strURL As String = _
              "http://www.directnic.com/whois/index.php?query=" _
              + txtDomain.Text
            Dim web As New WebClient()
            ' byte array to store the extracted bufData by webclient

            Dim bufData As Byte()
            bufData = web.DownloadData(strURL)
            ' got the bufData now convert it into string form

            firstLevelbufData = Encoding.Default.GetString(bufData)
            ' this exception will be fired when the host name

            ' is not resolved or any other connection problem

        Catch ex As System.Net.WebException
            txtResult.Text = ex.Message()
            Exit Sub
        End Try
        Try
            ' first and last are the regular expression string

            ' for extraction bufData witnin two tags

            ' you can change according to your requirement

            Dim first, last As String
            ' chr(34) is used for (") symbol

            first = "<p class=  " + Chr(34) + "text12" + Chr(34) +">"
            last = "</p>"
            Dim RE As New Regex(first + _
              "(?<MYDATA>.*?(?=" + last + "))", _
              RegexOptions.IgnoreCase Or RegexOptions.Singleline)
            ' try to extract the bufData  within the first and last tag

            Dim m As Match = RE.Match(firstLevelbufData)
            ' got the result

            txtResult.Text = m.Groups("MYDATA").Value + ""
            ' check if no information abour that domain is available

            If txtResult.Text.Length < 10 Then _
              txtResult.Text = _
                "Information about this domain is not available !!"
        Catch e3 As Exception
            txtResult.Text = "Sorry the whois information" & _ 
                                 " is currently not available !!"
        End Try
    End Sub

End Class

详细说明

演示项目包含

  • WhoIs.aspx - 该类。
  • WhoIs.aspx.vb:代码隐藏页面。
© . All rights reserved.