ยินดีต้อนรับคุณ,
บุคคลทั่วไป
กรุณา
เข้าสู่ระบบ
หรือ
ลงทะเบียน
ส่งอีเมล์ยืนยันการใช้งาน?
พฤศจิกายน 22, 2024, 21:44:06 PM
หน้าแรก
ช่วยเหลือ
ค้นหา
Recent posts
เข้าสู่ระบบ
สมัครสมาชิก
BMS-HOSxP Community
»
HOSxP
»
Delphi / Pascal
»
ฝาก อ.วุฒิ
« หน้าที่แล้ว
ต่อไป »
พิมพ์
หน้า: [
1
]
ลงล่าง
ผู้เขียน
หัวข้อ: ฝาก อ.วุฒิ (อ่าน 4586 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
doramon
บุคคลทั่วไป
ฝาก อ.วุฒิ
«
เมื่อ:
เมษายน 21, 2010, 19:11:41 PM »
0
http://delphi.about.com/library/adppagescrap.zip
ทำเสร็จเอามาให้ผมใช้ด้วยนะครับ
โค๊ด: Delphi
{
Article:
Simple HTML page scraping with Delphi
http://delphi.about.com/library/weekly/aa062502a.htm
This article will show you the techniques nedded to
download an HTML page from the Internet, do some page
scraping (regular expressions for pattern matching)
and finally present the information in more
*situation-friendly* manner.
Download the ZIPed version.
}
MainForm
.
DFM
Select Form1
,
Select View
As
Text
,
Paste the text into Editor
,
Select View
As
Form
.
object
Form1
:
TForm1
Left
=
240
Top
=
166
Width
=
290
Height
=
245
Caption
=
'Form1'
Color
=
clBtnFace
Font
.
Charset
=
DEFAULT_CHARSET
Font
.
Color
=
clWindowText
Font
.
Height
=
-
11
Font
.
Name
=
'MS Sans Serif'
Font
.
Style
=
[
]
OldCreateOrder
=
False
PixelsPerInch
=
96
TextHeight
=
13
object
Button1
:
TButton
Left
=
8
Top
=
8
Width
=
75
Height
=
25
Caption
=
'Button1'
TabOrder
=
0
OnClick
=
Button1Click
end
object
ListView1
:
TListView
Left
=
0
Top
=
40
Width
=
282
Height
=
178
Align
=
alBottom
Anchors
=
[
akLeft
,
akTop
,
akRight
,
akBottom
]
Columns
=
<
item
Caption
=
'Title'
end
item
Caption
=
'URL'
end
item
Caption
=
'Description'
end
item
Caption
=
'When/Where'
end>
TabOrder
=
1
ViewStyle
=
vsReport
end
end
UNIT1
.
PAS
unit
Unit1
;
interface
uses
Windows
,
Messages
,
SysUtils
,
Variants
,
Classes
,
Graphics
,
Controls
,
Forms
,
Dialogs
,
ComCtrls
,
StdCtrls
,
extactns
;
type
TForm1
=
class
(
TForm
)
Button1
:
TButton
;
ListView1
:
TListView
;
procedure
Button1Click
(
Sender
:
TObject
)
;
private
{ Private declarations }
public
{ Public declarations }
end
;
var
Form1
:
TForm1
;
implementation
{$R *.dfm}
function
Download_HTM
(
const
sURL
,
sLocalFileName
:
string
)
:
boolean
;
begin
Result
:
=
True
;
with
TDownLoadURL
.
Create
(
nil
)
do
try
URL
:
=
sURL
;
Filename
:
=
sLocalFileName
;
try
ExecuteTarget
(
nil
)
;
except
Result
:
=
False
end
;
finally
Free
;
end
;
end
;
procedure
TForm1
.
Button1Click
(
Sender
:
TObject
)
;
const
ADPNEWHOTURL
=
'http://delphi.about.com/cs/newandhot/index.htm'
;
TmpFileName
=
'c:\temp_adp.newandhot'
;
var
rawHTM
,
buffer
,
DataBuf
,
ItemBuf
:
string
;
sTitle
,
sLink
,
sDesc
,
SWhenWhere
:
string
;
NewAndHotHTMFile
:
TextFile
;
iStart
,
iStop
:
integer
;
stop
:
boolean
;
begin
if
NOT
Download_HTM
(
ADPNEWHOTURL
,
TmpFileName
)
then
begin
ShowMessage
(
'Error in HTML file download'
)
;
Exit
;
end
;
//read the HTML file
AssignFile
(
NewAndHotHTMFile
,
TmpFileName
)
;
try
Reset
(
NewAndHotHTMFile
)
;
while
not
EOF
(
NewAndHotHTMFile
)
do
begin
ReadLn
(
NewAndHotHTMFile
,
buffer
)
;
rawHTM
:
=
Concat
(
rawHTM
,
buffer
)
;
end
;
//START find the new and hot data on the page located between <!--DCnt--> and </!--DCnt-->
iStart
:
=
Pos
(
'<!--DCnt-->'
,
rawHTM
)
+
Length
(
'<!--DCnt-->'
)
;
iStop
:
=
Pos
(
'<!--/DCnt-->'
,
rawHTM
)
;
DataBuf
:
=
Copy
(
rawHTM
,
iStart
,
iStop
-
iStart
)
;
if
DataBuf
=
''
then
begin
with
ListView1
.
Items
.
Add
do
begin
Caption
:
=
'Format error'
;
SubItems
.
Add
(
ADPNEWHOTURL
)
;
SubItems
.
Add
(
'The HTML file with the news is not formated properly!'
)
;
SubItems
.
Add
(
'in '
+
TmpFileName
)
;
end
;
Exit
;
end
;
//STOP find the new and hot data on the page located between <!--DCnt--> and </!--DCnt-->
//start remove any script inside the htm page
stop
:
=
False
;
repeat
iStart
:
=
Pos
(
'<script>'
,
DataBuf
)
;
if
iStart <>
0
then
begin
iStop
:
=
Pos
(
'</script>'
,
DataBuf
)
+
Length
(
'</script>'
)
;
DataBuf
:
=
Copy
(
DataBuf
,
0
,
iStart
-
1
)
+
Copy
(
DataBuf
,
iStop
,
MaxInt
)
;
end
else
stop
:
=
True
;
until
stop
;
//stop remove any script inside the htm page
ListView1
.
Clear
;
//start grab one item at a time (example):
(*
<p>
<a href="/library/bluc/ucvcl.htm"><b>FREE CODE VCL: stMasterComboBox</b></a><br>
<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.
</p>
*)
stop
:
=
False
;
repeat
iStart
:
=
Pos
(
'<p>'
,
DataBuf
)
+
Length
(
'<p>'
)
;
if
(
iStart <>
0
)
AND
(
DataBuf <>
''
)
then
begin
//find one item
iStop
:
=
Pos
(
'</p>'
,
DataBuf
)
;
ItemBuf
:
=
Copy
(
DataBuf
,
iStart
,
iStop
-
iStart
)
;
//remove that one
Delete
(
DataBuf
,
1
,
-
1
+
iStop
+
Length
(
'</p>'
)
)
;
//find the link
iStart
:
=
Pos
(
'<a href="'
,
ItemBuf
)
+
Length
(
'<a href="'
)
;
iStop
:
=
Pos
(
'">'
,
ItemBuf
)
;
sLink
:
=
'http://delphi.about.com'
+
Copy
(
ItemBuf
,
iStart
,
iStop
-
iStart
)
;
//find the title
iStart
:
=
Pos
(
'<b>'
,
ItemBuf
)
+
Length
(
'<b>'
)
;
iStop
:
=
Pos
(
'</b>'
,
ItemBuf
)
;
sTitle
:
=
Copy
(
ItemBuf
,
iStart
,
iStop
-
iStart
)
;
//date and location (When/Where)
iStart
:
=
Pos
(
'<i>'
,
ItemBuf
)
+
Length
(
'<i>'
)
;
iStop
:
=
Pos
(
'</i>'
,
ItemBuf
)
;
SWhenWhere
:
=
Copy
(
ItemBuf
,
iStart
,
iStop
-
iStart
)
;
iStart
:
=
Pos
(
'<a href="'
,
SWhenWhere
)
;
iStop
:
=
Pos
(
'">'
,
SWhenWhere
)
;
Delete
(
SWhenWhere
,
iStart
,
iStop
-
iStart
+
Length
(
'">'
)
)
;
SWhenWhere
:
=
StringReplace
(
SWhenWhere
,
'</a>'
,
''
,
[
]
)
;
//find the description
sDesc
:
=
Copy
(
ItemBuf
,
Pos
(
'</i>'
,
ItemBuf
)
+
Length
(
'</i>'
)
,
MaxInt
)
;
//add to list view
with
ListView1
.
Items
.
Add
do
begin
Caption
:
=
sTitle
;
SubItems
.
Add
(
sLink
)
;
SubItems
.
Add
(
sDesc
)
;
SubItems
.
Add
(
sWhenWhere
)
;
end
;
end
else
stop
:
=
True
;
until
stop
;
//Stop grab one item at a time:
finally
CloseFile
(
NewAndHotHTMFile
)
;
end
;
//delete the temp file
try
DeleteFile
(
TmpFileName
)
except
end
;
end
;
end
.
Project1
.
PAS
program
Project1
;
uses
Forms
,
Unit1
in
'Unit1.pas'
{Form1}
;
{$R *.res}
begin
Application
.
Initialize
;
Application
.
CreateForm
(
TForm1
,
Form1
)
;
Application
.
Run
;
end
.
{
********************************************
Zarko Gajic
About.com Guide to Delphi Programming
http://delphi.about.com
email: delphi.guide@about.com
free newsletter: http://delphi.about.com/library/blnewsletter.htm
forum: http://forums.about.com/ab-delphi/start/
********************************************
}
บันทึกการเข้า
พิมพ์
หน้า: [
1
]
ขึ้นบน
« หน้าที่แล้ว
ต่อไป »
BMS-HOSxP Community
»
HOSxP
»
Delphi / Pascal
»
ฝาก อ.วุฒิ