delphi网页数据抓取

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

delphi⽹页数据抓取简单:
IdHttp:TIdHttp;
Params:TStrings;
Begin
Params:=TStrinList.Create;
IdHttp:=TIdHttp.Create(Nil);
//提交⽹页的参数
Params.Add('参数1=XXX');
Params.Add('参数2=YYY');
Try
IdHttp.Get('你要的⽹址',Params);
Memo1.Text:=
Finlly
Params.Free;
IdHttp.Free;
end;
{你分析Memo1中的数据}
{将数据保存⾄数据库}
end;
function TLisReport.DownloadFile(const remoteurl: string; Stream: TStream;
DownloadTitle: string): Boolean;
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
BytesRead: DWORD;
Buffer: array[1..8192] of Char;
HaveRead: DWORD;
lpdwlen, lpdwidx, lpdword: DWord;
begin
Result := false;
NetHandle := InternetOpen(
'htmlcopy for Lis',
INTERNET_OPEN_TYPE_DIRECT,
nil,
nil,
0);
if Assigned(NetHandle) then begin
UrlHandle := InternetOpenUrl(
NetHandle,
PChar(remoteurl),
nil,
0,
INTERNET_FLAG_RELOAD,
0);
if Assigned(UrlHandle) then
begin
HaveRead := 0;
lpdword := 0;
lpdwlen := 4;
lpdwidx := 0;
HttpQueryInfo(UrlHandle, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @lpdword, lpdwlen, lpdwidx);
repeat
FillChar(Buffer, 8192, 0);
InternetReadFile(UrlHandle, @Buffer[1], SizeOf(Buffer), BytesRead);
Stream.Write(Buffer[1], bytesread);
HaveRead := HaveRead + bytesread;
hintProgress(DownloadTitle, round(HaveRead * 100 / lpdword));
until BytesRead = 0;
result := true;
hintProgress(DownloadTitle, 0);
end;
InternetCloseHandle(UrlHandle);
end;
InternetCloseHandle(NetHandle);
end;
⽆外乎两种⽅法:
1⽤http的控件来POST或者GET,这⾥要注意NMHTTP在POST时有缺陷,论坛⾥有论述,建议⽤ICS或者INDY
2⽤IHTMLDOCUMENT2来做,这种⽅法就是通过IE接⼝在程序⾥⾯进⾏提交
我曾经为某个⽹上评选活动作弊,两种投票器都作过,很简单的
我⽤的是 Delphi 7 和 Indy 的 TidHttp 控件
对于有Session的主页进⾏ Post时,会出现过期等字样
下⾯是程序,求⾼⼿指点
//----------------------------------------------------
procedure TMainForm.btnRunClick(Sender: TObject);
var
Source: TStrings;
Response: TMemoryStream;
S, Cookie: String;
i: integer;
begin
Response := TMemoryStream.Create;
Source := TStringList.Create;
S := Http.Get(Page1);
Memo1.Text := S;
// 从返回的页⾯中找出cookie, 并增加到http.Request中
for i := 0 to Http.Response.RawHeaders.Count -1 do
if UpperCase(LeftStr(Http.Response.RawHeaders[i], 10)) = 'SET-COOKIE' then
begin
Cookie := Trim(Copy(Http.Response.RawHeaders[i], 12, MaxInt));
Cookie := Copy(Cookie, 1, Pos(';', Cookie) - 1);
Http.Request.RawHeaders.Add('Cookie:'+ Cookie);
end;
// 再去下⼀页, 先准备数据
Source.Clear;
Source.Add('USERnumber=' + UserName + '&pwd=' + PassWord + '&UserType=0'
+ '&imageField2.x=19&imageField2.y=8');
Memo1.Text := Http.Post(Page2, Source);
Source.Free;
Response.Free;
//----------------------------------------------------。

相关文档
最新文档