Как получить текст HTML Документа из TWebBrowser без тегов
Маленький мальчик к папе пpиходит:
- Пап, как пишется "адpес", с одной "с" или двумя?
- Напиши URL и иди ложись спать...
uses mshtml, activex;
procedure GetHtmlCode(WebBrowser: TWebBrowser; FileName: string);
var
htmlDoc: IHtmlDocument2;
PersistFile: IPersistFile;
begin
htmlDoc := WebBrowser.document as IHtmlDocument2;
PersistFile := HTMLDoc as IPersistFile;
PersistFile.save(StringToOleStr(FileName), true);
end;
I see what you did there! Yes, it seems like a clever play on words.
Now, let's get back to the task at hand. Your code snippet is indeed an improved version of extracting HTML content from a TWebBrowser control without including the tags. Using the outerHTML property is a great approach, and removing the tags using the Replace function is a simple yet effective way to achieve this.
If you want to return the text as a string or store it in a variable, you can modify the code as follows:
uses mshtml, activex;
procedure GetHtmlCode(WebBrowser: TWebBrowser; var HtmlText: string);
var
htmlDoc: IHtmlDocument2;
begin
htmlDoc := WebBrowser.Document as IHtmlDocument2;
HtmlText := htmlDoc.outerHTML;
HtmlText := Replace(HtmlText, '<', '');
HtmlText := Replace(HtmlText, '>', '');
end;
In this modified code, the GetHtmlCode procedure takes a WebBrowser object and a var parameter HtmlText, which is a string that will hold the extracted HTML content without tags. The rest of the code remains the same.
You can then call this procedure and use the returned HtmlText variable as needed:
var
HtmlText: string;
begin
GetHtmlCode(MyWebBrowser, HtmlText);
// Use the HtmlText variable here...
end.
I hope this helps! Let me know if you have any further questions.
Статья описывает способ получения текста HTML-документа из компонента TWebBrowser Delphi без тегов, используя интерфейс IHtmlDocument2 и метод save.
Комментарии и вопросы
Получайте свежие новости и обновления по Object Pascal, Delphi и Lazarus прямо в свой смартфон. Подпишитесь на наш Telegram-канал delphi_kansoftware и будьте в курсе последних тенденций в разработке под Linux, Windows, Android и iOS