4
procedure TForm1.ExtractLinks(const URL: String; const StringList: TStringList);
{ Extract "href" attribute from A tags from an URL and add to a stringlist. }
var
  i: Integer;
  iDoc: IHTMLDocument2;
  iHTML: String;
  iv: Variant;
  iLinks: OleVariant;
  iDocURL: String;
  iURI: TidURI;
  iHref: String;
  iIdHTTP: TidHTTP;
  iListItem: TListItem;
begin
  StringList.Clear;
  ListView1.Clear;
  iURI := TidURI.Create(URL);
  try
    iDocURL := 'http://' + iURI.Host;
    if iURI.Path <> '/' then
      iDocURL := iDocURL + iURI.Path;
  finally
    iURI.Free;
  end;
  iDoc := CreateComObject(Class_HTMLDOcument) as IHTMLDocument2;
  try
    iDoc.DesignMode := 'on';
    while iDoc.ReadyState <> 'complete' do
      Application.ProcessMessages;
    iv := VarArrayCreate([0, 0], VarVariant);
    iIdHTTP := TidHTTP.Create(nil);
    try
      iHTML := iIdHTTP.Get(URL);
    finally
      iIdHTTP.Free;
    end;
    iv[0] := iHTML;
    iDoc.Write(PSafeArray(System.TVarData(iv).VArray));
    iDoc.DesignMode := 'off';
    while iDoc.ReadyState <> 'complete' do
      Application.ProcessMessages;
    iLinks := iDoc.All.Tags('A');
    if iLinks.Length > 0 then
    begin
      ListView1.Items.BeginUpdate;
      for i := 0 to -1 + iLinks.Length do
      begin
        iHref := iLinks.Item(i).href;
        if (iHref[1] = '/') then
          iHref := iDocURL + iHref
        else if Pos('about:', iHref) = 1 then
          iHref := iDocURL + Copy(iHref, 7, Length(iHref));
        if (IsValidURL(iHref)) and (IsKnownFormat(iHref)) then
        begin
          StringList.Add(iHref);
          iListItem := ListView1.Items.Add;
          iListItem.Caption := iHref;
        end;
        ListView1.Items.EndUpdate;
      end;
    end;
  finally
    iDoc := nil;
  end;
end;

procedure TForm1.GetLinks1Click(Sender: TObject);
var
  iUrlList: TStringList;
begin
  iUrlList := TStringList.Create;
  try
    { Get the url list }
    ExtractLinks(Url1.Text, iUrlList);
  finally
    iUrlList.Free;
  end;
end;

On some websites this code produces a list of image urls but on some websites it produces a 'HTTP/1.1 301 Moved Permanently' EIdHTTPProtocolException. Is it possible to get a list of Img urls from a web page url or am I doing something incorrectly?

whosrdaddy
  • 11,720
  • 4
  • 50
  • 99
Bill
  • 2,993
  • 5
  • 37
  • 71
  • 2
    I guess you're not handling redirects. Refer to this thread: http://stackoverflow.com/questions/4549809/indy-idhttp-how-to-handle-page-redirects – rsrx May 19 '14 at 16:44
  • 1
    Why the down vote? Are you supposed to know everything before you ask a question? I guess so, but if I did, then there would be no need for the question to begin with. – Bill May 19 '14 at 20:04
  • 1
    Because simply googling "301 http" yields you "URL redirection" topics. Then you already know that you lack redirection support in your code. Then googling "indy http redirection" returns you link I c/ped to you as first result. (disclaimer: I didnt downvoted you) – rsrx May 19 '14 at 20:22
  • 1
    There a lot of driveby downvoters here. +1 question is ok :) – whosrdaddy May 21 '14 at 12:50

1 Answers1

7

Set iIdHTTP.HandleRedirects := True so it starts automatically handling redirects.

whosrdaddy
  • 11,720
  • 4
  • 50
  • 99