淘宝的宝贝描述页是ajax异步载入的,所以直接idhttp读一下是弄不到它的源码的。用下面的方法就可以。
代码如下:
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls,SHDocVw, ExtCtrls, OleCtrls;
type TForm1 = class(TForm) Edit1: TEdit; Label1: TLabel; TabControl1: TTabControl; GO: TButton; Button1: TButton; Button2: TButton; Panel1: TPanel; RichEdit1: TRichEdit; Panel2: TPanel; WebBrowser1: TWebBrowser; Label2: TLabel; procedure GOClick(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormShow(Sender: TObject); procedure TabControl1Change(Sender: TObject); procedure Button1Click(Sender: TObject); ******* { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm} {$APPTYPE CONSOLE} uses mshtml;
function GetHtml(const WebBrowser:TWebBrowser): string; var Doc: IHTMLDocument2; begin Doc := WebBrowser.Document as IHTMLDocument2; if Assigned(Doc) and Assigned(Doc.body) then Result := Doc.body.OuterHtml; //也可以用innerHTML取得其HTML文本 end;
procedure TForm1.Button1Click(Sender: TObject); begin //读商品描述页html form1.RichEdit1.Text:=GetHtml(form1.WebBrowser1); form1.TabControl1.TabIndex:=0; form1.Panel1.BringToFront; end;
procedure TForm1.Button2Click(Sender: TObject); begin Application.Terminate; end;
procedure TForm1.FormShow(Sender: TObject); begin //显示控制 form1.TabControl1.TabIndex:=0; form1.Panel1.BringToFront; form1.Button1.Enabled:=false; WebBrowser1.Silent := True; //禁脚本错误
end;
procedure TForm1.GOClick(Sender: TObject); begin
form1.TabControl1.TabIndex:=1; form1.Panel2.BringToFront; form1.WebBrowser1.Navigate(trim(form1.Edit1.Text)); form1.Button1.Enabled:=true; end;
procedure TForm1.TabControl1Change(Sender: TObject); begin case TabControl1.TabIndex of 0:begin form1.Panel1.BringToFront; end; 1:begin form1.Panel2.BringToFront; end; end; end;
end.
界面代码:
object Form1: TForm1 Left = 229 Top = 150 BorderIcons = [biSystemMenu, biMinimize] Caption = #35835#28120#23453#21830#21697#25551#36848' '#29482#24735#33021 ClientHeight = 269 ClientWidth = 330 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False Position = poDesigned OnShow = FormShow PixelsPerInch = 96 TextHeight = 13 object Label1: TLabel Left = 8 Top = 19 Width = 23 Height = 13 Caption = 'URL:' end object Label2: TLabel Left = 15 Top = 242 Width = 156 Height = 13 Caption = #20808'go'#65292#31561#32593#39029#36733#20837#21518#20877#21462#28304#30721 Color = clGradientActiveCaption Font.Charset = DEFAULT_CHARSET Font.Color = clRed Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] ParentColor = False ParentFont = False end object Edit1: TEdit Left = 32 Top = 16 Width = 241 Height = 21 TabOrder = 0 Text = 'http://item.taobao.com/auction/item_detail-0db1-b65488f879057795' + '999a205b3ed15545.jhtml' end object TabControl1: TTabControl Left = 8 Top = 48 Width = 313 Height = 177 TabOrder = 1 Tabs.Strings = ( 'Html' 'View') TabIndex = 0 OnChange = TabControl1Change object Panel1: TPanel Left = 8 Top = 24 Width = 297 Height = 145 Caption = 'Panel1' TabOrder = 0 object RichEdit1: TRichEdit Left = 0 Top = 0 Width = 297 Height = 145 Font.Charset = GB2312_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] Lines.Strings = ( 'RichEdit1') ParentFont = False ScrollBars = ssVertical TabOrder = 0 end end object Panel2: TPanel Left = 8 Top = 24 Width = 302 Height = 150 Caption = 'Panel2' TabOrder = 1 object WebBrowser1: TWebBrowser Left = 0 Top = 0 Width = 297 Height = 145 TabOrder = 0 ControlData = { 4C000000B21E0000FC0E00000000000000000000000000000000000000000000 000000004C000000000000000000000001000000E0D057007335CF11AE690800 2B2E126208000000000000004C0000000114020000000000C000000000000046 8000000000000000000000000000000000000000000000000000000000000000 00000000000000000100000000000000000000000000000000000000} end end end object GO: TButton Left = 281 Top = 21 Width = 41 Height = 21 Caption = 'GO' TabOrder = 2 OnClick = GOClick end object Button1: TButton Left = 192 Top = 232 Width = 57 Height = 29 Caption = #21462#28304#30721 TabOrder = 3 OnClick = Button1Click end object Button2: TButton Left = 265 Top = 232 Width = 57 Height = 29 Caption = #36864#20986 TabOrder = 4 OnClick = Button2Click end end
源代码下载:http://www.rayfile.com/files/b844aba6-15d3-11df-95b8-0015c55db73d/
|
请发表评论