how to use TwebBrowser to navigate a lot of URLs

hello!everybody!
I have some trouble in dealing links,First,I got a lot of useful URls from a webpage,and then I want to navigate these links orderly in order to get the part of the text of these pages.So,I use the Ttimer component, interval:20 seconds.I thought that is enough.But in fact,
when I navigate the first page,I got the text which I want to get.However,the webbrowser did not work,I just don't kown why?who can help me?Thank you!
There is my code in Timer Event:
procedure TForm1.Timer1Timer(Sender: TObject);
var
i,j:integer;
title,text:string;
doc:IHtmlDocument2;
elec:IhtmlelementCollection;
TableElement:IhtmlElement;
begin
timer1.Enabled:=false;
title:=cv_title[cv_currweb];
webbrowser1.Navigate(cv_link[cv_currweb]);
while cv_flag=false do
application.ProcessMessages;
if cv_flag=true then
doc:=webbrowser1.document as Ihtmldocument2;
elec:=doc.all;
for i:=0 to elec.length-1 do
begin
tableelement:=elec.item(i,emptyparam) as IhtmlElement;
if tableelement.tagName='TABLE' then
begin
text:=tableelement.outerText;
if pos(title,text)>0 then
memo1.Text:=text;
end;
end;
inc(cv_currweb);
timer1.Enabled:=(cv_currweb<lenlink);
end;

Comments

  • : hello!everybody!
    : I have some trouble in dealing links,First,I got a lot of useful URls from a webpage,and then I want to navigate these links orderly in order to get the part of the text of these pages.So,I use the Ttimer component, interval:20 seconds.I thought that is enough.But in fact,
    : when I navigate the first page,I got the text which I want to get.However,the webbrowser did not work,I just don't kown why?who can help me?Thank you!
    : There is my code in Timer Event:
    : procedure TForm1.Timer1Timer(Sender: TObject);
    : var
    : i,j:integer;
    : title,text:string;
    : doc:IHtmlDocument2;
    : elec:IhtmlelementCollection;
    : TableElement:IhtmlElement;
    : begin
    : timer1.Enabled:=false;
    : title:=cv_title[cv_currweb];
    : webbrowser1.Navigate(cv_link[cv_currweb]);
    : while cv_flag=false do
    : application.ProcessMessages;
    : if cv_flag=true then
    : doc:=webbrowser1.document as Ihtmldocument2;
    : elec:=doc.all;
    : for i:=0 to elec.length-1 do
    : begin
    : tableelement:=elec.item(i,emptyparam) as IhtmlElement;
    : if tableelement.tagName='TABLE' then
    : begin
    : text:=tableelement.outerText;
    : if pos(title,text)>0 then
    : memo1.Text:=text;
    : end;
    : end;
    : inc(cv_currweb);
    : timer1.Enabled:=(cv_currweb<lenlink);
    : end;
    :
    It would make others easier to help you if you provide a better presentation of your code.
    Cheers.

  • there the whole units
    unit mainform;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ComCtrls, StdCtrls, OleCtrls, mshtml,SHDocVw, ExtCtrls;

    type
    TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    WebBrowser1: TWebBrowser;
    Memo1: TMemo;
    Button1: TButton;
    Timer1: TTimer;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure WebBrowser1BeforeNavigate2(Sender: TObject;
    const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
    Headers: OleVariant; var Cancel: WordBool);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure WebBrowser1DownloadComplete(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation
    uses commonvar;
    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    var i ,j,m:integer;
    doc:ihtmldocument2;
    link:ihtmlelement;
    begin
    m:=0;
    webbrowser1.Navigate('http://sh.house.sina.com.cn/news/index.html');
    while cv_flag=false do
    application.ProcessMessages;
    if cv_flag=true then
    doc:=webbrowser1.document as ihtmldocument2;
    for i:=0 to doc.links.length-1 do
    begin
    link:=doc.links.item(i,0) as ihtmlelement;
    j:=pos('2004-11-02',link.toString);
    if j>0 then
    begin
    setlength(cv_title,m+1);
    setlength(cv_link,m+1);
    cv_title[m]:=link.innerText;
    cv_link[m]:=link.toString;
    memo1.lines.Add(cv_title[m]);
    memo1.Lines.Add(cv_link[m]);
    inc(m);
    end;
    lenlink:=m;
    setlength(cv_content,m);
    end;
    end;

    procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;
    const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
    Headers: OleVariant; var Cancel: WordBool);
    begin
    cv_flag:=false;
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    memo1.Text:='';
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    cv_currweb:=0;
    timer1.Enabled:=true;
    end;


    procedure TForm1.Timer1Timer(Sender: TObject);
    var
    i,j:integer;
    title,text:string;
    doc:IHtmlDocument2;
    elec:IhtmlelementCollection;
    TableElement:IhtmlElement;
    begin
    timer1.Enabled:=false;
    title:=cv_title[cv_currweb];
    webbrowser1.Navigate(cv_link[cv_currweb]);
    while cv_flag=false do
    application.ProcessMessages;
    if cv_flag=true then
    //memo1.Text:=(webbrowser1.document as Ihtmldocument2).body.outertext;
    begin
    doc:=webbrowser1.document as Ihtmldocument2;
    elec:=doc.all;
    for i:=0 to elec.length-1 do
    begin
    tableelement:=elec.item(i,emptyparam) as IhtmlElement;
    if tableelement.tagName='TABLE' then
    begin
    text:=tableelement.outerText;
    if pos(title,text)>0 then
    begin
    memo1.Text:=text;

    end;
    end;
    end;
    inc(cv_currweb);
    timer1.Enabled:=(cv_currweb<lenlink);
    end;
    end;

    procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
    begin
    cv_flag:=true;
    end;

    end.




    and the CommonVar unit:
    unit commonvar;

    interface
    var
    cv_flag:boolean;
    cv_title:array of string;
    cv_link:array of string;
    cv_content:array of string;
    lenlink:integer;
    cv_currweb:integer;
    implementation

    end.


    Firstly,I want to get the links of that page,then I want to navigate these URLs which I got.


    Thank you!Mr delphiasker!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories