CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

关于一个文件内容替换的问题,急!大家帮忙!!!!

楼主tiandiqing(天地情缘)2004-12-01 12:31:21 在 VB / 基础类 提问

我现在的文件是htm的,里面的内容大致如下:  
   
   
  '//========================================================  
  <html>  
  <head>  
  <title>大家好</title>  
  <meta   http-equiv="Content-Type"   content="text/html;   charset=gb2312">  
  </head>  
   
  <body   bgcolor="#009900"   text="#000000">  
  <table   width="1000"   border="1"   cellspacing="0"   cellpadding="0">  
      <tr>  
          <td>大家好,同志辛苦了<a   href='www.sohu.com'>你好</a></td>  
      </tr>  
  </table>  
  </body>  
  </html>  
   
  //=======================================================================  
   
  我想把多余的东西替换掉,替换完成后是这样的  
   
   
  <body   bgcolor="#009900"   text="#000000">  
  大家好,同志辛苦了<a   href='www.sohu.com'>你好</a>  
  </body>  
   
  table里的东西都不要,但连接和北京颜色保留,也就是说<body>   </body>   <a   href=''>sdfsd</a>  
  这样的东西和内容要保留,大家帮我搞一下吧,分不够可以加!  
   
   
   
  问题点数:0、回复次数:6Top

1 楼aohan(aohan)回复于 2004-12-01 12:36:58 得分 0

先顶一下Top

2 楼langkew(拉倒)回复于 2004-12-01 12:46:47 得分 0

曾经做了一个专用于提取网页中元素的函数。可以作为你的参考:  
  用法示例:  
  dim   tmp   as   string  
  tmp=GetHrefs("http://www.163.com","<a   href=","</a>")      
   
  后面几个参数分别是:  
  PrevResults:上一次检查的结果,用于输出好几个页面中不同的结果。  
  AllowReduplicate:是否允许重复的结果出现。  
  Keywords:只搜索相关关键字的结果  
   
  Private   Function   GetHrefs(ByVal   strURL   As   String,   _  
                                                      ByVal   StartMark   As   String,   _  
                                                      ByVal   EndMark   As   String,   _  
                                                      Optional   ByVal   PrevResults   As   String,   _  
                                                      Optional   ByVal   AllowReduplicate   As   Boolean,   _  
                                                      Optional   ByVal   Keywords   As   String)   as   String  
                                                   
          On   Error   GoTo   ErrHandle:  
           
          Dim   strTmp   As   String  
          Dim   strHTML  
          Dim   Results   As   Variant  
          Dim   tmpResult   As   String  
          strHTML   =   Inet1.OpenURL(strURL)  
           
          Dim   CurrentURL   As   String  
          CurrentURL   =   strURL  
   
           
          Dim   pos0   As   Long  
          Dim   pos1   As   Long  
          Dim   posS   As   Long  
          Dim   posE   As   Long  
           
          posS   =   1  
          posE   =   1  
           
           
          If   PrevResults   <>   ""   Then  
                  tmpResult   =   PrevResults  
          End   If  
   
           
          Do   While   InStr(posS,   strHTML,   StartMark,   vbTextCompare)   >   0  
                  If   InStr(Inet1.GetHeader,   "HTTP/1.1   404")   >   0   Then  
                          Exit   Do  
                  ElseIf   InStr(Inet1.GetHeader,   "HTTP/1.1   401")   >   0   Then  
                          Exit   Do  
                  ElseIf   InStr(Inet1.GetHeader,   "HTTP/1.1   403")   >   0   Then  
                          Exit   Do  
                  End   If  
                  pos0   =   InStr(posS,   strHTML,   StartMark,   vbTextCompare)  
                  posE   =   pos0   +   1  
                  pos1   =   InStr(posE,   strHTML,   EndMark,   vbTextCompare)  
                  If   pos0   >   0   Then  
                          pos0   =   pos0   +   Len(StartMark)  
                          strTmp   =   Mid(strHTML,   pos0,   pos1   -   pos0)  
                  End   If  
                   
                  'URL的后期处理  
                  If   InStr(strTmp,   Chr(32))   >   0   Then  
                          strTmp   =   Left(strTmp,   InStr(strTmp,   Chr(32)))  
                  End   If  
                  strTmp   =   Replace(strTmp,   Chr(34),   "")  
                  strTmp   =   Replace(strTmp,   "'",   "")  
                  strTmp   =   Replace(strTmp,   "#",   "")  
                  strTmp   =   Trim(strTmp)  
                   
                  Dim   strProtocol   As   String  
                  strProtocol   =   GetUrlParts(strTmp,   1)  
   
                  If   strProtocol   =   ""   Then  
                          strTmp   =   CombineURL(GetFullPath(CurrentURL),   strTmp)  
                  End   If  
                   
                   
                  '如果不是HTTP协议的链接  
                  If   (GetUrlParts(strTmp,   1)   <>   "http")   And   (chkNoScan.Value   =   Unchecked)   Then  
                          'nothing  
                  ElseIf   (InStr(strTmp,   Keywords)   <=   0)   And   (chkNoScan.Value   =   Unchecked)   Then  
                          'nothing  
                  ElseIf   (InStr(strTmp,   "?")   <=   0)   And   (chkNoScan.Value   =   Unchecked)   Then           '过滤不带参数的URL  
                          'nothing  
                  ElseIf   InStr(strTmp,   Chr(13))   >   0   Then             '过滤带回车的URL  
                          'nothing  
                  Else  
                          '查看是否有图片  
                          'If   InStr("gif,jpg,bmp,png",   LCase(Right(strTmp,   3)))   <=   0   Then  
                          If   InStr("gif,jpg,bmp,png",   LCase(Right(strTmp,   3)))   <=   0   Then  
                                  If   AllowReduplicate   =   True   Then  
                                          tmpResult   =   tmpResult   &   strTmp   &   vbCrLf  
                                  Else  
                                          '查看以前的URL中是否有重复记录  
                                          Dim   strTmp1   As   String  
                                          strTmp1   =   Replace(strTmp,   GetUrlParts(strTmp,   6),   "")  
                                          If   InStr(1,   LCase(tmpResult),   LCase(strTmp1),   vbTextCompare)   <=   0   Then  
                                                  tmpResult   =   tmpResult   &   strTmp   &   vbCrLf  
                                          End   If  
                                  End   If  
                          End   If  
                  End   If  
                   
                  posS   =   pos0  
                  posE   =   pos1   +   Len(EndMark)  
                   
   
          Loop  
           
          GetHrefs   =   tmpResult  
          Exit   Function  
  ErrHandle:  
   
          GetHrefs   =   ""  
           
  End   FunctionTop

3 楼HtoFire(冬天里的一把火)回复于 2004-12-01 17:16:51 得分 0

主要用Instr函数吧,思考一种算法,琢一分析,应该没问题的。Top

4 楼tiandiqing(天地情缘)回复于 2004-12-01 17:21:07 得分 0

大家帮帮忙吧Top

5 楼Plutoxkxu(★深渊★)回复于 2004-12-01 18:57:53 得分 0

学习Top

6 楼True1024()回复于 2004-12-01 18:59:16 得分 0

UPTop

相关问题

  • 求文件内容查找和替换汇编源程序,急!!!
  • 请教文件内容替换问题~~~~~~~~~~~~~~!!!!!!!
  • 请问一个问题:怎么替换文件中的内容啊,很着急的
  • 一个有关替换二进制文件中某部分内容的问题!急用!!
  • 如何对WORD文件当中的内容进行替换
  • 如何替换HTML文件里面BODY标签内的内容?
  • 如何替换文件中的一行内容?
  • 如何替换文件中某一定长度的二进制内容?
  • 请问如何写入文本文件时替换原有内容?
  • 关于读取一个JSP文件,替换其内容的问题!!!!!!!!!!

关键词

得分解答快速导航

  • 帖主:tiandiqing

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo