Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 11-27-2006, 02:31 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default Read data from Windows Internet Explorer.

I am trying to read data from IE. This data comes in a table similar to excel sheet. Any idea how to do that?

Regards,
Abdulaziz Jasser
Reply With Quote
  #2  
Old 11-27-2006, 04:49 PM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: Read data from Windows Internet Explorer.

OK, first thing to do is look at my previous post here:
http://www.delphipages.com/threads/t...66842&G=166808

Then, modify it so that the stringgrid has the same amount
of columns as the HTML table you are trying to parse.
Then to get the data from IE, instead of via idHTTP, change these lines:

[delphi]
idhttp := TidHTTP.Create(Self);
try
if AB then
begin
LHTML := idHTTP.Get(HTMLUrlab);
end
else
begin
LHTML := idHTTP.Get(HTMLUrlab);
end;
finally
idhttp.Free;
end;
Idoc:=CreateComObject(Class_HTMLDocument) as IHTMLDocument2;
try
IDoc.designMode:='on';
while IDoc.readyState<>'complete' do
Application.ProcessMessages;
v:=VarArrayCreate([0,0],VarVariant);
v[0]:= LHTML;
IDoc.write(PSafeArray(System.TVarData(v).VArray));
IDoc.designMode:='off';
while IDoc.readyState<>'complete' do
Application.ProcessMessages;

{TO THIS:}
iDoc := GetIEDocument(URL);
{and add this function}
// Gets the HTMLDocument from IE that matches the passed URL
function GetIEDocument(URL: String): IHTMLDocument2;
var
ShellWin: TShellWindows;
i: integer;
IE: IWebBrowser2;
begin
ShellWin := TShellWindows.Create(nil);
try
for i := 0 to ShellWin.Count-1 do
begin
try
ShellWin.Item(i).QueryInterface(IID_IWebBrowser2, IE);
if IE.LocationURL = URL then
Result := IE.Document as IHTMLDocument2;
except
end;
end;
finally
ShellWin.Free;
end;
end;
[/delphi]


If you need more help, let me know.


MrBaseball34
Hook'Em Horns!

2005 College Football National Champions
2005 College Baseball National Champions
Reply With Quote
  #3  
Old 11-28-2006, 03:21 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default RE: Read data from Windows Internet Explorer.

YOU WILL NOT BELIVE IT...but I am trying to read the stock market prices and data in real-time from the same URL. I am trying to build my own database to create some reports which I believe will help me do better trading in the Saudi stock market. I really appreciate if you can modify the project to do that.

Big thanks...

Regards,
Abdulaziz Jasser
Reply With Quote
  #4  
Old 11-28-2006, 03:55 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: Read data from Windows Internet Explorer.

Can you not see how I'm doing it and modify it yourself?

MrBaseball34
Hook'Em Horns!

2005 College Football National Champions
2005 College Baseball National Champions
Reply With Quote
  #5  
Old 11-28-2006, 06:23 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: Read data from Windows Internet Explorer.

Do you want it retrieved the same way, .vs via Internet
Explorer?

MrBaseball34
Hook'Em Horns!

2005 College Football National Champions
2005 College Baseball National Champions
Reply With Quote
  #6  
Old 11-28-2006, 06:31 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default RE: Read data from Windows Internet Explorer.

Which one is better (faster, easier, more reliable...etc)?

Regards,
Abdulaziz Jasser
Reply With Quote
  #7  
Old 11-28-2006, 06:38 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default RE: Read data from Windows Internet Explorer.

This could be a personal question, but do you trade in the Saudi stock market? Another thing, for which reason did you build that project? I am asking this because I am interested to know how to use the collected data to generate some useful reports for better trading in the Saudi stock market. I've already lost a big amount of money in it since February 2006 and I don't want to lose more.

Regards,
Abdulaziz Jasser
Reply With Quote
  #8  
Old 11-28-2006, 06:41 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default RE: Read data from Windows Internet Explorer.

For some privacy, you could reply to me using my email:

jasser@cyberia.net.sa


Regards,
Abdulaziz Jasser
Reply With Quote
  #9  
Old 11-28-2006, 07:57 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: Read data from Windows Internet Explorer.

OK, here it is. I will note though that there are some
javascript errors occuring when loading the HTML into
the IHTMLDocument2. It will not happen if we "grab" it
from IE, however, the code to grab the one from IE is
significantly slower.

I do not trade in the SA market, I am in the U.S. I did
this for the point for the original poster of the question
noted in my first post, who, BTW, did not even bother to
accept my post as an answer. Oh, also, if you choose to
use the Arabic site, you will have to reverse your loops
to do the R-L stuff.


[delphi]
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, COMObj, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP, MSHTML, ActiveX,
ShellAPI, ShDocVw;

const
HTMLUrlen = 'http://www.tadawul.com.sa/wps/portal/!ut/p/.cmd/cs/.ce' +
'/7_0_A/.s/7_0_4AI/_s.7_0_A/7_0_4AI/.cmd/ChangeLanguage/.l/en';
HTMLUrlab = 'http://www.tadawul.com.sa/wps/portal/!ut/p/.cmd/cs/.ce/' +
'7_0_A/.s/7_0_4AI/_s.7_0_A/7_0_4AI/.cmd/ChangeLanguage/.l/ar';

type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
procedure GetTableData(AB: Boolean);
public
{ Public declarations }
FromIE: Boolean;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
i, x: Integer;
begin
StringGrid1.ColWidths[1] := 108;
x := (StringGrid1.Width-165) div 13;
for i := 2 to 14 do
begin
StringGrid1.ColWidths[i] := x;
end;
StringGrid1.ColWidths[0] := x;
StringGrid1.Cells[0, 0] := 'Symbol';
StringGrid1.Cells[1, 0] := 'Company';
StringGrid1.Cells[2, 0] := 'Last Price';
StringGrid1.Cells[3, 0] := 'Last Vol';
StringGrid1.Cells[4, 0] := 'Last Change Val';
StringGrid1.Cells[5, 0] := 'Last Change %';
StringGrid1.Cells[6, 0] := 'Cum # Trades';
StringGrid1.Cells[7, 0] := 'Cum Vol';
StringGrid1.Cells[8, 0] := 'Best Bid Price';
StringGrid1.Cells[9, 0] := 'Best Bid Vol';
StringGrid1.Cells[10, 0] := 'Best Off Price';
StringGrid1.Cells[11, 0] := 'Best Off Vol';
StringGrid1.Cells[12, 0] := 'Today Open';
StringGrid1.Cells[13, 0] := 'Today High';
StringGrid1.Cells[14, 0] := 'Today Low';
StringGrid1.Cells[15, 0] := 'Link';
StringGrid1.ColWidths[15] := -1;
// change this to False to get using idHTTP
// or True to get from an open IE Window pointing to
// the URL you want to parse; it MUST be one of the
// two URLs above.
FromIE := True;
end;

function GetIEDocument(URL: String): IHTMLDocument2;
var
ShellWin: TShellWindows;
i: integer;
IE: IWebBrowser2;
begin
ShellWin := TShellWindows.Create(nil);
try
for i := 0 to ShellWin.Count-1 do
begin
try
ShellWin.Item(i).QueryInterface(IID_IWebBrowser2, IE);
if IE.LocationURL = URL then
Result := IE.Document as IHTMLDocument2;
except
end;
end;
finally
ShellWin.Free;
end;
end;

procedure TForm1.GetTableData(AB: Boolean);
var
i, j: Integer;
idhttp: TidHTTP;
LHTML: String;
IDoc: IHTMLDocument2;
v: Variant;
TableRows: IHTMLElementCollection;
TableRow: IHTMLTableRow;
TableCell: IHTMLElement;
LSymbol: String;
LPos1: Integer;
LPos2: Integer;
LLinkText: String;
LInnerText: String;
ovLinks: IHTMLElementCollection;
sURL: String;
ovLink: IHTMLAnchorElement;
N: WideString;
begin
try
if not FromIE then
begin
idhttp := TidHTTP.Create(Self);
try
if AB then
begin
LHTML := idHTTP.Get(HTMLUrlab);
end
else
begin
LHTML := idHTTP.Get(HTMLUrlen);
end;
finally
idhttp.Free;
end;
LHTML := StringReplace(LHTML, 'setSelectedNode', '//setSelectedNode',
[rfReplaceAll]);
Idoc:=CreateComObject(Class_HTMLDocument) as IHTMLDocument2;
IDoc.designMode:='on';
while IDoc.readyState<>'complete' do
Application.ProcessMessages;
v:=VarArrayCreate([0,0],VarVariant);
v[0]:= LHTML;
IDoc.write(PSafeArray(System.TVarData(v).VArray));
IDoc.designMode:='off';
while IDoc.readyState<>'complete' do
Application.ProcessMessages;
end
else
begin
if AB then
iDoc := GetIEDocument(HTMLUrlab)
else
iDoc := GetIEDocument(HTMLUrlen);
end;
if iDoc <> nil then
begin
// Get all tha tablerows
TableRows := (iDoc.all.tags('TR') as IHTMLElementCollection);
for i := 0 to TableRows.length-1 do
begin
TableRow := (TableRows.item(i, 0) as IHTMLTableRow);
if (Trim((TableRow as IHTMLELement).className) = 'regular') or
(Trim((TableRow as IHTMLELement).className) = 'table_back') then
begin
if TableRow.cells.length > 2 then
begin
for j := 0 to TableRow.cells.length-1 do
begin
TableCell := (TableRow.cells.item(j,0) as IHTMLElement);
if j = 0 then
begin
ovLinks := TableCell.all as IHTMLElementCollection;
ovLinks := ovLinks.tags('A') as IHTMLElementCollection;
if ovLinks.Length > 0 then
begin
N := '';
if Supports(ovLinks.item(0, 0), IHTMLAnchorElement, ovLink) then
begin
sURL := ovLink.href;
sURL := StringReplace(sURL, 'about:blank', 'http://www.tadawul.com.sa',
[rfReplaceAll, rfIgnoreCase]);
sURL := StringReplace(sURL, '&', '&&', [rfReplaceAll, rfIgnoreCase]);
StringGrid1.Cells[15, StringGrid1.RowCount-1] := sURL;
end;
end; {if ovLinks.Length > 0}
LLinkText := TableCell.outerHTML;
LLinkText := StringReplace(LLinkText, '&', '&', [rfReplaceAll]);
LLinkText := StringReplace(LLinkText, #13#10, '', [rfReplaceAll]);
if AB then
begin
// Replace the encoded soft hyphen char
LLinkText := StringReplace(LLinkText, '*', '*', [rfReplaceAll]);
end; {if AB}
LPos1 := Pos('symbol=', LLinkText)+7;
LInnerText := TableCell.innerText;
if AB then
begin
// Replace the encoded soft hyphen char
LInnerText := StringReplace(LInnerText, '*', '*',
[rfReplaceAll]);
end; {if AB}
LPos2 := Pos('">'+ LInnerText, LLinkText)-LPos1;
LSymbol := Copy(LLinkText, Lpos1, LPos2);
StringGrid1.Cells[j, StringGrid1.RowCount-1] := LSymbol;
StringGrid1.Cells[j+1, StringGrid1.RowCount-1] :=
TableCell.innerText;
end
else
begin
StringGrid1.Cells[j+1, StringGrid1.RowCount-1] :=
TableCell.innerText;
end; {if j = 0}
end; {for j := 0 to TableRow.cells.length-1 do}
StringGrid1.RowCount := StringGrid1.RowCount + 1;
end; {if TableRow.cells.length > 2}
end; {if (Trim((TableRow as IHTMLELement).className...}
end; {for i := 0 to TableRows.length-1 do}
end; {if iDoc <> nil}
finally
IDoc := nil;
// Remove the last empty row;
StringGrid1.RowCount := StringGrid1.RowCount -1;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
GetTableData(False);
end;

{DrawTheText courtesy of Ralph Friedman}
procedure DrawTheText(ACanvas: TCanvas; ARect: TRect;
AValue: string; AAlign: TAlignment);
var
horzOffset: integer;
options: integer;
vertOffset: integer;
begin
// Note: The Handle property of TCanvas is
// the handle of its DC.
with ACanvas do begin
vertOffset := ARect.Top + (((ARect.Bottom - ARect.Top) -
TextExtent(AValue).CY) div 2);
horzOffset := TextExtent('Mi').CX div 4;
options := ETO_CLIPPED or ETO_OPAQUE;
case AAlign of
taLeftJustify:
begin
SetTextAlign(Handle, TA_LEFT or TA_TOP or TA_NOUPDATECP);
ExtTextOut(Handle, ARect.Left + horzOffset, vertOffset, options,
@ARect, PChar(AValue), Length(AValue), nil);
end;
taRightJustify:
begin
SetTextAlign(Handle, TA_RIGHT or TA_TOP or TA_NOUPDATECP);
ExtTextOut(Handle, ARect.Right - horzOffset, vertOffset, options,
@ARect, PChar(AValue), Length(AValue), nil);
end;
taCenter:
begin
horzOffset := ((ARect.Right - ARect.Left) - TextExtent(AValue).CX) div 2;
SetTextAlign(Handle, TA_LEFT or TA_TOP or TA_NOUPDATECP);
ExtTextOut(Handle, ARect.Left + horzOffset, vertOffset, options,
@ARect, PChar(AValue), Length(AValue), nil);
end;
end;
end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
Grid: TStringGrid;
theText: String;
align: TAlignment;
begin
Grid := TStringGrid(Sender);
align := taLeftJustify;
if (ARow > 0) and (Grid.Cells[0, ARow] <> '') then
begin
case ACol of
1:
begin
theText := Grid.Cells[ACol, ARow];
Grid.Canvas.Brush.Color := clWindow;
Grid.Canvas.FillRect(Rect);
Grid.Canvas.Font.Color := clBlue;
Grid.Canvas.Font.Style := [fsUnderline];
DrawTheText(Grid.Canvas, Rect, theText, align);
end; {case: 1}
4, 5:
begin
theText := Grid.Cells[ACol, ARow];
Grid.Canvas.Brush.Color := clWindow;
Grid.Canvas.FillRect(Rect);
if StrToFloat(Grid.Cells[ACol, ARow]) > 0.0 then
Grid.Canvas.Font.Color := clGreen
else
Grid.Canvas.Font.Color := clRed;
DrawTheText(Grid.Canvas, Rect, theText, align);
end; {case: 4,5}
15:
begin
Grid.Canvas.Brush.Color := clWindow;
Grid.Canvas.FillRect(Rect);
end; {case: 15}
end; {case}
end; {if}
end;

procedure BrowseTo(sURL: String);
var
sei : TShellExecuteInfo;
begin
ZeroMemory(@sei, sizeof(sei));
with sei do
begin
cbSize := SizeOf(sei);
fMask := SEE_MASK_NOCLOSEPROCESS;
Wnd := Form1.Handle;
lpVerb := 'open';
// pass url to lpFile parameter to open in default browser
lpFile := PChar(sURL);
nShow := SW_SHOWNORMAL;
end;
ShellExecuteEX(@sei);
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
sURL: String;
Grid: TStringGrid;
begin
Grid := TStringGrid(Sender);
if (ACol = 1) and (ARow > 0) then
begin
sURL := Grid.Cells[15, ARow];
BrowseTo(sURL);
end;
end;

procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
Grid: TStringGrid;
begin
Grid := TStringGrid(Sender);
if Grid.MouseCoord(X, Y).X = 1 then
Screen.Cursor := crHandPoint
else
Screen.Cursor := crDefault;
end;

end.
[/delphi]

MrBaseball34
Hook'Em Horns!

2005 College Football National Champions
2005 College Baseball National Champions
Reply With Quote
  #10  
Old 11-28-2006, 09:49 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default RE: Read data from Windows Internet Explorer.

It's late here in Riyadh, but I will test the code tomorrow in. However, still don't see from the other post how you chose the Saudi market to give an example. BTW: from the poster name I can tell that he is from India.

Thanks...

Regards,
Abdulaziz Jasser
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 04:23 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.