频道分类

Delphi Net.HttpClient用最精简的代码获取网页数据

作者:admin 来源: 日期:2020/9/30 8:35:09 人气: 标签:

 
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Classes,
  System.Net.HttpClient,
  System.RegularExpressions;

const
  url = 'http://www.delphitop.com/';
begin
  var http := THttpClient.Create;
  var sl := TStringList.Create;
  try
    sl.LoadFromStream(http.Get(url).ContentStream, TEncoding.ANSI);
    var m := TRegEx.Match(sl.Text, '<title>(.*?)</title>');
    if m.Success then
     writeln('获取信息: ', m.Value);
      Readln;
  finally
    sl.Free;
    http.Free;
  end;
end.