Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得;“文本”;html页面的格式?(Webbrowser-Delphi)_Html_Delphi_Text_Browser - Fatal编程技术网

如何获得;“文本”;html页面的格式?(Webbrowser-Delphi)

如何获得;“文本”;html页面的格式?(Webbrowser-Delphi),html,delphi,text,browser,Html,Delphi,Text,Browser,我正在使用WebBrowser获取html页面的源代码。 我们的页面源代码有一些文本和一些html标记。像这样: FONT></P><P align=center><FONT color=#ccffcc size=3>**Hello There , This is a text in our html page** </FONT></P>&am

我正在使用WebBrowser获取html页面的源代码。 我们的页面源代码有一些文本和一些html标记。像这样:

FONT></P><P align=center><FONT color=#ccffcc size=3>**Hello There , This is a text in our html page** </FONT></P><P align=center> </P>

Html标记是随机的,我们无法猜测它们。那么,有没有办法只获取文本并将其与html标记分离?

您应该考虑使用如果星号是常量,您只需获取
**
之间的每个字符。 如果星号不是常量,您可以重写此字符串并删除所有标记(从
开始,以
结束的标记)。或者您可以使用一些DOM解析器库来执行此操作。

您可以使用实例来解析并从html代码中选择明文

看到这个样本了吗

uses
MSHTML,
SHDocVw,
ActiveX;

function GetPlainText(Const Html: string): string;
var
DummyWebBrowser: TWebBrowser;
Document       : IHtmlDocument2;
DummyVar       : Variant;
begin
   Result := '';
   DummyWebBrowser := TWebBrowser.Create(nil);
   try
     //open an blank page to create a IHtmlDocument2 instance
     DummyWebBrowser.Navigate('about:blank');
     Document := DummyWebBrowser.Document as IHtmlDocument2; 
     if (Assigned(Document)) then //Check the Document
     begin
       DummyVar      := VarArrayCreate([0, 0], varVariant); //Create a variant array to write the html code to the  IHtmlDocument2
       DummyVar[0]   := Html; //assign the html code to the variant array
       Document.Write(PSafeArray(TVarData(DummyVar).VArray)); //set the html in the document
       Document.Close;
       Result :=(Document.body as IHTMLBodyElement).createTextRange.text;//get the plain text
     end;
   finally
     DummyWebBrowser.Free;
   end;
end;

本质上:一般来说你不能

HTML是一种标记语言,它有着广泛的用途和令人难以置信的动态更改内容的可能性,几乎不可能做到这一点(看看web浏览器供应商需要付出多大的努力才能通过acid测试)。因此,您只能做一个子集

对于特定且定义良好的HTML子集,您有更好的机会:

首先,您需要在 字符串,然后解析该HTML

例如,可以使用Indy获取HTML(请参阅的答案)

解析在很大程度上取决于您的HTML,可能非常复杂,您可以尝试或

您可以按照RRuz的建议使用TWebBrowser,但这取决于Internet Explorer。
现代Windows系统不保证安装Internet Explorer


--jeroen使用Delphi HTML组件库仅从HTML文档获取文本非常简单。
THtDocument.InnerText属性返回不带标记的格式化文本。

谢谢。我需要更多说明(问题也已编辑)谢谢。但使用此函数,结果是:“FONT>

**您好,这是我们html页面中的文本**

”.html标记仍然在这里。通过嵌套函数解决了:GetPlainText(GetPlainText(MyString));:D谢谢先生Piruz@RRUZ使用此函数,我可以从德语Umlaut字符(ü、ß等)中获得有趣的字符(ü、ß等)。如何解决此问题?嗨,Jeroen,我使用的是嵌入式WebBrowser componenet,没有问题:)直到您在未安装Internet Explorer的计算机上运行软件;那么它就会失败。这可能不是问题,但这是你需要注意的。