ผู้เขียน หัวข้อ: ฝาก อ.วุฒิ  (อ่าน 4586 ครั้ง)

0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

doramon

  • บุคคลทั่วไป
ฝาก อ.วุฒิ
« เมื่อ: เมษายน 21, 2010, 19:11:41 PM »
0
http://delphi.about.com/library/adppagescrap.zip

ทำเสร็จเอามาให้ผมใช้ด้วยนะครับ

 :)


โค๊ด: Delphi
  1.  
  2. {
  3. Article:
  4. Simple HTML page scraping with Delphi
  5.  
  6. http://delphi.about.com/library/weekly/aa062502a.htm
  7.  
  8. This article will show you the techniques nedded to
  9. download an HTML page from the Internet, do some page
  10. scraping (regular expressions for pattern matching)
  11. and finally present the information in more
  12. *situation-friendly* manner.
  13.  
  14. Download the ZIPed version.
  15. }
  16.  
  17.  
  18. MainForm.DFM
  19.  
  20.  
  21. Select Form1,
  22. Select View As Text,
  23. Paste the text into Editor,
  24. Select View As Form.
  25.  
  26.  
  27.  
  28. object Form1: TForm1
  29.   Left = 240
  30.   Top = 166
  31.   Width = 290
  32.   Height = 245
  33.   Caption = 'Form1'
  34.   Color = clBtnFace
  35.   Font.Charset = DEFAULT_CHARSET
  36.   Font.Color = clWindowText
  37.   Font.Height = -11
  38.   Font.Name = 'MS Sans Serif'
  39.   Font.Style = []
  40.   OldCreateOrder = False
  41.   PixelsPerInch = 96
  42.   TextHeight = 13
  43.   object Button1: TButton
  44.     Left = 8
  45.     Top = 8
  46.     Width = 75
  47.     Height = 25
  48.     Caption = 'Button1'
  49.     TabOrder = 0
  50.     OnClick = Button1Click
  51.   end
  52.   object ListView1: TListView
  53.     Left = 0
  54.     Top = 40
  55.     Width = 282
  56.     Height = 178
  57.     Align = alBottom
  58.     Anchors = [akLeft, akTop, akRight, akBottom]
  59.     Columns = <
  60.       item
  61.         Caption = 'Title'
  62.       end
  63.       item
  64.         Caption = 'URL'
  65.       end
  66.       item
  67.         Caption = 'Description'
  68.       end
  69.       item
  70.         Caption = 'When/Where'
  71.       end>
  72.     TabOrder = 1
  73.     ViewStyle = vsReport
  74.   end
  75. end
  76.  
  77. UNIT1.PAS
  78.  
  79.  
  80. unit Unit1;
  81.  
  82. interface
  83.  
  84. uses
  85.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  86.   Dialogs, ComCtrls, StdCtrls, extactns;
  87.  
  88. type
  89.   TForm1 = class(TForm)
  90.     Button1: TButton;
  91.     ListView1: TListView;
  92.     procedure Button1Click(Sender: TObject);
  93.   private
  94.     { Private declarations }
  95.   public
  96.     { Public declarations }
  97.   end;
  98.  
  99. var
  100.   Form1: TForm1;
  101.  
  102. implementation
  103.  
  104. {$R *.dfm}
  105.  
  106.  
  107.  
  108. function Download_HTM(const sURL, sLocalFileName:string): boolean;
  109. begin
  110.   Result:=True;
  111.   with TDownLoadURL.Create(nil) do
  112.   try
  113.     URL:=sURL;
  114.     Filename:=sLocalFileName;
  115.     try
  116.       ExecuteTarget(nil);
  117.     except
  118.       Result:=False
  119.     end;
  120.   finally
  121.     Free;
  122.   end;
  123. end;
  124.  
  125. procedure TForm1.Button1Click(Sender: TObject);
  126. const
  127.   ADPNEWHOTURL = 'http://delphi.about.com/cs/newandhot/index.htm';
  128.   TmpFileName='c:\temp_adp.newandhot';
  129. var
  130.   rawHTM, buffer, DataBuf, ItemBuf : string;
  131.   sTitle, sLink, sDesc, SWhenWhere : string;
  132.   NewAndHotHTMFile : TextFile;
  133.   iStart, iStop : integer;
  134.   stop: boolean;
  135. begin
  136.   if NOT Download_HTM(ADPNEWHOTURL,TmpFileName) then
  137.   begin
  138.     ShowMessage('Error in HTML file download');
  139.     Exit;
  140.   end;
  141.  
  142.     //read the HTML file
  143.     AssignFile(NewAndHotHTMFile, TmpFileName);
  144.     try
  145.       Reset(NewAndHotHTMFile);
  146.       while not EOF(NewAndHotHTMFile) do begin
  147.         ReadLn(NewAndHotHTMFile, buffer);
  148.         rawHTM := Concat(rawHTM, buffer);
  149.       end;
  150.  
  151.       //START find the new and hot data on the page located between  <!--DCnt--> and </!--DCnt-->
  152.       iStart := Pos('<!--DCnt-->',rawHTM) + Length('<!--DCnt-->');
  153.       iStop := Pos('<!--/DCnt-->',rawHTM);
  154.       DataBuf := Copy(rawHTM, iStart, iStop-iStart);
  155.       if DataBuf='' then
  156.       begin
  157.         with ListView1.Items.Add do
  158.         begin
  159.           Caption:= 'Format error';
  160.           SubItems.Add(ADPNEWHOTURL);
  161.           SubItems.Add('The HTML file with the news is not formated properly!');
  162.           SubItems.Add('in ' + TmpFileName);
  163.         end;
  164.         Exit;
  165.       end;
  166.       //STOP find the new and hot data on the page located between  <!--DCnt--> and </!--DCnt-->
  167.  
  168.       //start remove any script inside the htm page
  169.       stop:=False;
  170.       repeat
  171.         iStart := Pos('<script>',DataBuf) ;
  172.         if iStart <> 0 then
  173.         begin
  174.           iStop := Pos('</script>', DataBuf) + Length('</script>');
  175.           DataBuf := Copy(DataBuf, 0, iStart - 1) + Copy(DataBuf, iStop, MaxInt);
  176.         end
  177.         else
  178.           stop:=True;
  179.       until stop;
  180.       //stop remove any script inside the htm page
  181.  
  182.       ListView1.Clear;
  183.  
  184.       //start grab one item at a time (example):
  185.       (*
  186.       <p>
  187.       <a href="/library/bluc/ucvcl.htm"><b>FREE CODE VCL: stMasterComboBox</b></a><br>
  188.       <i>06/10 in <a href="/library/bluc/ucvcl.htm">FREE VCL</a>.</i> stMasterComboBox is a combo box component that helps you to create master-detail forms supplying navigation with a combo box.
  189.       </p>
  190.       *)
  191.       stop:=False;
  192.       repeat
  193.         iStart := Pos('<p>',DataBuf) + Length('<p>') ;
  194.         if (iStart <> 0) AND (DataBuf <> '') then
  195.         begin
  196.           //find one item
  197.           iStop := Pos('</p>', DataBuf);
  198.           ItemBuf := Copy(DataBuf, iStart, iStop - iStart);
  199.  
  200.           //remove that one
  201.           Delete(DataBuf, 1, -1 + iStop + Length('</p>'));
  202.  
  203.           //find the link
  204.           iStart:=Pos('<a href="',ItemBuf) + Length('<a href="');
  205.           iStop:=Pos('">',ItemBuf);
  206.           sLink:= 'http://delphi.about.com' +  Copy(ItemBuf, iStart, iStop-iStart);
  207.  
  208.           //find the title
  209.           iStart:=Pos('<b>',ItemBuf) + Length('<b>');
  210.           iStop:=Pos('</b>',ItemBuf);
  211.           sTitle:= Copy(ItemBuf, iStart, iStop-iStart);
  212.  
  213.           //date and location (When/Where)
  214.           iStart:=Pos('<i>',ItemBuf) + Length('<i>');
  215.           iStop:=Pos('</i>',ItemBuf);
  216.           SWhenWhere := Copy(ItemBuf, iStart, iStop-iStart);
  217.           iStart:=Pos('<a href="',SWhenWhere);
  218.           iStop:=Pos('">',SWhenWhere);
  219.           Delete(SWhenWhere, iStart, iStop - iStart + Length('">'));
  220.           SWhenWhere := StringReplace(SWhenWhere,'</a>','',[]);
  221.  
  222.           //find the description
  223.           sDesc:=Copy(ItemBuf,Pos('</i>',ItemBuf) + Length('</i>'),MaxInt);
  224.  
  225.           //add to list view
  226.           with ListView1.Items.Add do
  227.           begin
  228.             Caption:= sTitle;
  229.             SubItems.Add(sLink);
  230.             SubItems.Add(sDesc);
  231.             SubItems.Add(sWhenWhere);
  232.           end;
  233.         end
  234.         else
  235.           stop:=True;
  236.       until stop;
  237.     //Stop grab one item at a time:
  238.     finally
  239.       CloseFile(NewAndHotHTMFile);
  240.     end;
  241.  
  242.     //delete the temp file
  243.     try
  244.       DeleteFile(TmpFileName)
  245.     except
  246.     end;
  247.  
  248. end;
  249.  
  250. end.
  251.  
  252. Project1.PAS
  253.  
  254.  
  255. program Project1;
  256.  
  257. uses
  258.   Forms,
  259.   Unit1 in 'Unit1.pas' {Form1};
  260.  
  261. {$R *.res}
  262.  
  263. begin
  264.   Application.Initialize;
  265.   Application.CreateForm(TForm1, Form1);
  266.   Application.Run;
  267. end.
  268.  
  269.  
  270. {
  271. ********************************************
  272. Zarko Gajic
  273. About.com Guide to Delphi Programming
  274. http://delphi.about.com
  275. email: delphi.guide@about.com
  276. free newsletter: http://delphi.about.com/library/blnewsletter.htm
  277. forum: http://forums.about.com/ab-delphi/start/
  278. ********************************************
  279. }
  280.  
  281.