用什么方法可以提取 .html 文件里面的内容
或者是将 .html 还原成源码存到 .txt文件中去,然后再提取我所需要的特定内容。
各位大哥,给个思路,最好能举个例子,谢谢!! 分数不是问题

解决方案 »

  1.   

    procedure TForm1.Button2Click(Sender: TObject);
    begin
      //WebBrowser1.Navigate(PChar('http://www.yiwant.com/cgi-bin/sn.cgi'),
      WebBrowser1.Navigate(PChar('http://www.yiwant.com/shouji114.htm'),
                            EmptyParam,EmptyParam,EmptyParam,EmptyParam);
      IsFind:= false;
    end;procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    var
      i: Integer;
      IDoc: IHTMLDocument2;
      iELC : IHTMLElementCollection ;
      S : string;
      HtmlInputEle : IHTMLInputElement;
      HtmlEle: IHTMLElement;
      //HtmlSelEle : IHTMLSelectElement;
      spDisp: IDispatch;
      //HtmlEleCol: IHTMLElementCollection;
        ovDoc: oleVariant;
    begin
      IDoc:= WebBrowser1.Document as IHTMLDocument2;
      iELC:= IDoc.Get_all;
      for i:=0 to iELC.length-1 do
      begin
        Application.ProcessMessages;
        Sleep(10);
        spDisp:= iELC.item(i,0);
        if SUCCEEDED(spDisp.QueryInterface(IHTMLInputElement ,HtmlInputEle)) then
        begin
          s:= HtmlInputEle.name;      if ((HtmlInputEle.type_ = 'text') and
              (HtmlInputEle.name = 'keyword')) then
          begin
            HtmlInputEle.value:= Edit1.Text;
          end;
          if ((HtmlInputEle.type_ = 'submit') and
              (HtmlInputEle.value = '提交')) then
          begin
            HtmlInputEle.form.submit;
            s:=HtmlInputEle.form.action;
            //Memo1.Lines.Add(s);
          end;
        end;
      end;
      if SUCCEEDED(spDisp.QueryInterface(IHTMLElement, HTMLEle)) then
        begin
          //Memo2.Lines.Add(HTMLEle.className);
          ovDoc:= (Sender as TWebBrowser).Document;
          s:= ovDoc.All.item.innerhtml;
          //Memo2.Text:= s;
          Label1.Caption:= SearchPlace(s);
          //if s <>'' then
          //WebBrowser1.Stop;
        end;
    end;function TForm1.SearchPlace(sPlace: String): String;
    var
      iPos: Integer;
      s: String;
    begin
      //s:= 'sorry,Nothing!';
      iPos:= pos(Edit1.Text,sPlace);
      if iPos > 0 then
      begin
        sPlace:= Copy(sPlace,iPos,Length(sPlace));
        iPos:= Pos('align=center>',sPlace);
        if iPos > 0 then
        begin
          sPlace:= Copy(sPlace,iPos+13 ,Length(sPlace));
          iPos:= Pos('</P></TD>',sPlace);
          if iPos > 0 then
          begin
            s:= Copy(sPlace,1,iPos-1);
          end;
        end;
      end;
      if s<>'' then WebBrowser1.Stop;
      if s = '没搞错吗?那真是不好意思了。' then
        s:= 'sorry,没有找到!';
      IsFind:= false;
      Result:= s;
    end;以上是我做的一个查手机所属地的关键源代码,其实现算法很简单
    先找到网页中的edit和button控件,然后辅值后提交(submit),页面显示完成后得到整个网页的源代码,然后通过字符串分析得到手机的归属地,你看看,应该对你有用的
      

  2.   

    比如说个一网页 .html 文件的,我想在delphi程序中打开它,
    以提取该网页中一些所需要的文字,要是能够将 .html 文件还原成源码提取出来存为 .txt文件 然后对 txt文件进行操作也可以,拜托,帮帮忙!!!!
      

  3.   

    duduwolf(嘟嘟狼  谢谢还有没有其他人能够再说点,!!!
      

  4.   

    HTML就是文本文件,不需要转换,改扩展文件名即可上面的例子实际上是用MS提供的COM接口,相关MSHTML的内容查询MSDN,利用它可以分析提取HTML的元素
      

  5.   

    : jsandy(江苏安迪)  大哥  在delphi程序中怎么实现这一点呢
      

  6.   

    ehom(?!)   谢谢   受教了
      

  7.   

    你的 .HTML 文件是否已经在硬盘上存在了?如果是,这样打开:先放一个 MEMO 控件,
    然后:Memo1.Lines.LoadFromFile('c:\lhp.html');