All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
抓取网页中的链接_[Asp.Net教程]

Writer: delv Article type: Programming skills(编程技巧) Time: 2014/1/23 3:14:55 Browse times: 389 Comment times: 0

抓取网页中的链接_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

抓取网页中的链接_[Asp.Net教程]























输入一个地址,就可以把那个网页中的链接提取出来,下面这段代码可以轻松实现,主要的是用到了正则表达式。




GetUrl.aspx代码如下:




<%@ Page Language="vb" CodeBehind="GetUrl.aspx.vb" AutoEventWireup="false" Inherits="aspxWeb.GetUrl" %>









http://lucky_elove.www1.dotnetplayground.com/

onClick="scrapeButton_Click" id="scrapeButton" runat="server">






Width="100%" Height="400">




后代码GetUrl.aspx.vb如下:




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 " Web 窗体设计器生成的代码 "




'该调用是 Web 窗体设计器所必需的。
Private Sub InitializeComponent()




End Sub




Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub




#End Region




Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Label1.Text = "请输入一个URL地址:"
scrapeButton.Text = "分离Href链接"
End Sub
Private report 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( _
"\foundAnchor>[^'"">]+?)[^>]*?\>", _
RegexOptions.Multiline Or RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)




Dim newWebPage As String = linksExpression.Replace(webPage, myDelegate)




TipResult.Text = "

从 " & urlTextBox.Text & "分离出的Href链接

" & _
"找到并整理" & countOfMatches.ToString() & " 个链接

" & _
report.ToString().Replace(Environment.NewLine, "
")
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




report.AppendFormat( _
"Link {0}, 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





来源: 孟宪会之精彩世界















































There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.