Asp. NET implements crawling links in Web pages

Source: Internet
Author: User
asp.net| link | Web page input An address, you can take the link in that page to extract, the following code can be easily implemented, the main is the use of regular expressions.

The geturl.aspx code is as follows:

<%@ Page language= "<a href= http://dev.21tx.com/language/vb/" target= "_blank" >VB</a> "codebehind=" GetUrl.aspx.vb "autoeventwireup=" false "inherits=" Aspx<a href= "http://dev.21tx.com/web/" target= "_blank" >web </a>. GetUrl "%>


<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<form id= "Form1" method= "POST" runat= "Server" >
<P>
<asp:label id= "Label1" runat= "Server" ></asp:Label>
<asp:textbox id= "Urltextbox" runat= "Server" width= "336px" >
http://lucky_elove.www1.dotnetplayground.com/
</asp:TextBox>
<asp:button id= "Scrapebutton" runat= "Server" ></asp:Button>
</P>
<P>
<asp:label id= "Tipresult" runat= "Server" ></asp:Label>
<asp:textbox id= "ResultLabel" runat= "Server" textmode= "MultiLine"
Width= "100%" height= "></asp:TextBox>"
</P>
</form>
</body>

Post code GetUrl.aspx.vb as follows:

Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Text.RegularExpressions
Imports System

Public Class GETURL
Inherits System.Web.UI.Page
Protected WithEvents Label1 as System.Web.UI.WebControls.Label
Protected WithEvents Urltextbox as System.Web.UI.WebControls.TextBox
Protected WithEvents Scrapebutton as System.Web.UI.WebControls.Button
Protected WithEvents Tipresult as System.Web.UI.WebControls.Label
Protected WithEvents ResultLabel as System.Web.UI.WebControls.TextBox

#Region the code generated by the Web Forms Designer

' This call is required by the Web Forms Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

End Sub

Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' CodeGen: This method call is required by the Web Forms Designer
' Do not modify it using the Code Editor.
InitializeComponent ()
End Sub

#End Region

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
Label1.Text = "Please enter a URL address:"
Scrapebutton.text = "Detach href link"
End Sub
Private as New StringBuilder ()
Private webpage as String
Private Countofmatches as Int32

Public Sub Scrapebutton_click (ByVal sender as System.Object, ByVal e as System.EventArgs)
Webpage = Graburl ()
Dim MyDelegate as New matchevaluator (AddressOf matchhandler)

Dim linksexpression as New Regex (_
"\<a.+?href=['" "] (?!) http\:\/\/) (?! mailto\:) (>foundanchor>[^ ' "" >]+?) [^>]*?\> ", _
Regexoptions.multiline or Regexoptions.ignorecase or regexoptions.ignorepatternwhitespace)

Dim newwebpage as String = Linksexpression.replace (webpage, mydelegate)

Tipresult.text = ""<b> Find and Organize" & Countofmatches.tostring () & "link </b><br><br>" & _
. ToString (). Replace (Environment.NewLine, "<br>")
Tipresult.text &= "Resultlabel.text = Newwebpage
End Sub

Public Function Matchhandler (ByVal m as Match) as String
Dim link as String = m.groups ("Foundanchor"). Value
Dim Rtol as New Regex ("^", regexoptions.multiline Or Regexoptions.righttoleft)
Dim col, row as Int32
Dim Linebegin as Int32 = Rtol.match (webpage, m.index). Index

row = Rtol.matches (webpage, m.index). Count
Col = M.index-linebegin

. AppendFormat (_
"Link <b>{0}</b>, fixed at row: {1}, col: {2}{3}", _
Server.HTMLEncode (m.groups (0). Value), _
Row, _
Col, _
Environment.NewLine _
)
Dim NewLink as String
If link. StartsWith ("/") Then
NewLink = link. Substring (1)
Else
NewLink = link
End If

Countofmatches + 1
Return m.groups (0). Value.replace (link, NewLink)
End Function

Private Function Graburl () as String
Dim WC as New WebClient ()
Dim s as Stream = WC. OpenRead (Urltextbox.text)
Dim sr as StreamReader = New StreamReader (s, System.Text.Encoding.Default)
Graburl = Sr. ReadToEnd
S.close ()
Wc. Dispose ()
End Function

End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.