delphi的httpclient库能够操作http和https链接。应该是XE以后新出的库。试了试用来抓网页还行。里面使用了HtmlParserEx库。只支持xe3以后的版本,看说明还能支持其他平台。该库使我们能够调用css选择器或xpath来取得我们需要的数据。
program 获取图片链接; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, System.Net.HttpClient, HtmlParserEx in 'HtmlParserEx.pas'; const url:string='https://www.italika.mx/Modelos/Trabajo/'; var stext:string; LHtml:IHtmlElement; LList:IHtmlElementList; begin try var http:THTTPClient:=THTTPClient.Create; http.UserAgent:='Mozilla/5.0'; stext:=http.Get(url).ContentAsString(); LHtml:=ParserHtml(stext); if LHtml <> nil then begin LList := LHtml.Find('li.display_tCell img'); for LHtml in LList do Writeln('url:', lhtml.Attributes['src']); end; http.Free; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; readln; end.
通过选择器处理后取得图片链接。效果如图:
得到图片链接可以尝试下载网络图。完整代码如下:
program 获取图片链接; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, System.Net.HttpClient,Vcl.Imaging.pngimage,Vcl.Graphics, HtmlParserEx in 'HtmlParserEx.pas'; const url:string='https://www.italika.mx/Modelos/Trabajo/'; var stext:string; LHtml:IHtmlElement; LList:IHtmlElementList; pngImg:TpngImage; i:Cardinal; begin try var http:THTTPClient:=THTTPClient.Create; pngImg:=TpngImage.Create; i:=0; http.UserAgent:='Mozilla/5.0'; stext:=http.Get(url).ContentAsString(); LHtml:=ParserHtml(stext); if LHtml <> nil then begin LList := LHtml.Find('li.display_tCell img'); for LHtml in LList do begin inc(i); pngImg.LoadFromStream(http.Get(lhtml.Attributes['src']).ContentStream); pngImg.SaveToFile('C:\Users\FanXiaoLei\Desktop\vb6regfreecom\'+ inttostr(i)+'.png'); end; end; pngImg.Free; http.Free; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; readln; end.
网络图片下载到了本地:
HtmlParserEx库下载:https://files.cnblogs.com/files/FanXiaoLei/HtmlParserEx.zip