菜鸟提问:如何取得页面的所有链接(正则表达式)

xiaofenguser 2004-01-18 03:30:09
string html = "<html><a href=first.htm>first tag text</a><a href=next.htm>next tag text</a></html>";
string p = @"<A[^>]*?HREF\s*=\s*[""']?([^'"" >]+?)[ '""]?>";

MatchCollection mc = Regex.Matches(html, p, RegexOptions.IgnoreCase);
IEnumerator ienum = mc.GetEnumerator();
while ( ienum.MoveNext() ) {
Match m = (Match) ienum.Current;
CaptureCollection cc = m.Captures;
for ( int k = 0; k < cc.Count; k++ ) {
Capture c = cc[k];
Console.WriteLine( c.ToString() );
}
}
结果只能得到:
<a href=first.htm>
<a href=next.htm>

我想要的结果是:
first.htm
next.htm
请问:我该怎么做?
...全文
102 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xiaofenguser 2004-01-18
谢谢 cg1120II(代码最优化第二版-§.NET专家§) ,你的速度可真快,问题已经OK了:)
  • 打赏
  • 举报
回复
cg1120II 2004-01-18
GetUrl.aspx代码如下:

<%@ Page Language="vb" CodeBehind="GetUrl.aspx.vb" AutoEventWireup="false" Inherits="aspxWeb.GetUrl" %>
<html>
<head>
<META http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<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 OnClick="scrapeButton_Click" id="scrapeButton" runat="server"></asp:Button>
</P>
<HR width="100%" SIZE="1">
<P>
<asp:Label id="TipResult" runat="server"></asp:Label>
<asp:TextBox id="resultLabel" runat="server" TextMode="MultiLine"
Width="100%" Height="400"></asp:TextBox>
</P>
</form>
</body>
</html>
后代码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 窗体设计器所必需的。
<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: 此方法调用是 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( _
"\<a.+?href=['""](?!http\:\/\/)(?!mailto\:)(?>foundAnchor>[^'"">]+?)[^>]*?\>", _
RegexOptions.Multiline Or RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)

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

TipResult.Text = "<h2>从 " & urlTextBox.Text & "分离出的Href链接</h2>" & _
"<b>找到并整理" & countOfMatches.ToString() & " 个链接</b><br><br>" & _
report.ToString().Replace(Environment.NewLine, "<br>")
TipResult.Text &= "<h2>整理过的页面</h2><script>window.document.title='抓取网页中的链接'</script>"
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 <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

  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.7w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-01-18 03:30
社区公告

让您成为最强悍的C#开发者