选择显示字体大小

抓取网页中的链接

     输入一个地址,就可以把那个网页中的链接提取出来,下面这段代码可以轻松实现,主要的是用到了正则表达式。
  
  geturl.aspx代码如下:
  
  <%@ 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" %>
  <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.do.netplayground.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
  
  
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons