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.
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
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