我正在使用 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 标签分开?
最佳答案
你可以使用 TWebBrowser从 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 - 如何获取html页面的 "Text"? (网络浏览器 - 德尔福),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666392/