惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
雷峰网
雷峰网
博客园 - 叶小钗
C
Check Point Blog
F
Fortinet All Blogs
A
About on SuperTechFans
Y
Y Combinator Blog
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
云风的 BLOG
云风的 BLOG
U
Unit 42
博客园_首页
量子位
M
MIT News - Artificial intelligence
G
Google Developers Blog
小众软件
小众软件
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
S
Schneier on Security
T
Tor Project blog
F
Full Disclosure
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
Security Latest
Security Latest
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 司徒正美
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
Recorded Future
Recorded Future
L
Lohrmann on Cybersecurity
I
Intezer
L
LangChain Blog
L
LINUX DO - 热门话题

博客园 - 绯村剑心

日期对象ToString方法格式符的大小写对输出结果是有影响的 - 绯村剑心 - 博客园 Ajax基础配置 — XMLHttpRequest 让文本输入框只能输入数字 - 绯村剑心 - 博客园 检测客户端显示器分辨率、浏览器类型和客户端IP 在ASP.NET 中实现单点登录(利用Cache, 将用户信息保存在服务器缓存中) 对于长时间装载的ASP.NET页面如何在客户端浏览器中显示进度? 带图片的,多列的DropDownList的实现 - 绯村剑心 - 博客园 一个很不错介绍session的文章 无法从Web服务器获取项目文件 曼彻斯特码与差分曼彻斯特码 如何清除SQL server日志 sql server日志文件总结及日志满的处理办法 SQL SERVER 安装问题详解 Server实用操作小技巧集合 从html中提出纯文本 用JAVASCRIPT来刷新框架子页面的七种方法。 创建高度动态变化的Iframe 上传图片并生成缩略图 ASP.NET程序中常用的三十三种代码
抓取网页中的链接
绯村剑心 · 2006-10-10 · via 博客园 - 绯村剑心

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

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> " & _ "<strong>找到并整理" & countOfMatches.ToString() & " 个链接</strong><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 <strong>{0}</strong>, 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