Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Como criar uma funcão que retorne HTML?

Post Reply
luiz_antoniosp
Posts: 8
Joined: Sat Jun 24, 2023 5:28 am

Como criar uma funcão que retorne HTML?

Post by luiz_antoniosp »

Tenho visto alguns tópicos sobre extração de html, porem nao achei nada que me ajuda se
preciso criar uma função , aonde eu passo a URL e retorna o HTML da pagina , em alguns caso
eu tenho que abrir parte dinamica da pagina para obter extração correta do html .
então eu peço ajuda de toda de como posso fazer isto, com o componente Tchomium....
fiz o teste no WB ele funciona bem porem nao consigo simular o click para abrir dinamincamente parte da pagina
fato que eu consigo com o Tchromium tranquilamente.

então veja o que eu preciso segue um exemplo , qualquer ajuda e muito bem vinda...
-------------------------------------------------------------------------------------------
Html := GetHTML(WebBrowser1,Edt_link_filter.Text);
-----------------------------------------------------------------------------------------
function GetHTML(WB: TWebBrowser; const URL: string): string;
begin
Result := '';
WB_Navigate(WB, URL);
WaitForBrowser(WB);
if WB_DocumentLoaded(WB) then
Result := (WB.Document as IHTMLDocument2).body.innerHTML;
end;
***************************************************************************************************
User avatar
salvadordf
Posts: 4056
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Como criar uma funcão que retorne HTML?

Post by salvadordf »

Hi,

Read this post to know how to get the HTML from a document :
https://www.briskbard.com/forum/viewtopic.php?p=7939#p7939

Please, write in English or Spanish. Use an on-line translator.
luiz_antoniosp
Posts: 8
Joined: Sat Jun 24, 2023 5:28 am

Re: Como criar uma funcão que retorne HTML?

Post by luiz_antoniosp »

Thank you for your attention, but obligatorily obtaining an html only within an event does not work for me, I need it free. To use it in several code snippets within a function, for example return within a button
User avatar
salvadordf
Posts: 4056
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Como criar uma funcão que retorne HTML?

Post by salvadordf »

Chromium uses several processes and threads to navigate and to render the web contents.
This means that many Chromium methods like TChromium.LoadURL and TChromium.RetrieveHTML are asynchronous.

You can use a TEvent to wait for the results as you can see in the ConsoleBrowser2 demo but this is not recommended.
luiz_antoniosp
Posts: 8
Joined: Sat Jun 24, 2023 5:28 am

Re: Como criar uma funcão que retorne HTML?

Post by luiz_antoniosp »

unfortunately although Crhomuim is a good component, I see it as a major flaw because all delphi components that allow access to url , html return without this limitation that does not make the slightest sense of obligation within a component event, hopefully the your developer fix this serious flaw
luiz_antoniosp
Posts: 8
Joined: Sat Jun 24, 2023 5:28 am

Re: Como criar uma funcão que retorne HTML?

Post by luiz_antoniosp »

Segue a solução para este problema ,deixo aqui pois pode muito ajudar a comunidade..

//----------------------------------Loading Page------------------------------------------------------//
Chromium1.LoadURL(https://www.google.com/);
Wait time .....

//----------------------------------function ------------------------------------------------------//

function TForm1.GetHTML(const Chromium: TChromium): string;
var
SourceHtml: string;
CefStringVisitor: ICefStringVisitor;
PSourceHtml: ^string;
begin
PSourceHtml := @SourceHtml;

CefStringVisitor := TCefFastStringVisitor.Create(
procedure(const str: ustring)
begin
PSourceHtml^ := str;
end);
Chromium.Browser.MainFrame.GetSource(CefStringVisitor);
Sleep(800);
Application.ProcessMessages;
// ShowMessage(SourceHtml);
Result := SourceHtml;
end;

//----------------------------------To call ------------------------------------------------------//
procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(Chromium1.Browser) then
Memo1.Text := GetHTML(Chromium1);
end;
Student
Posts: 72
Joined: Tue Aug 07, 2018 9:20 am

Re: Como criar uma funcão que retorne HTML?

Post by Student »

Why go to all this trouble, there's a ready-made method Chromium1.RetrieveHTML
Post Reply