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;
//----------------------------------------------------  



你可以通过这个链接引用该篇文章:http://chinamxm.bokee.com/viewdiary.13104228.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值