频道分类

Delphi Net.HttpClient正则批量获取网页代码中的数值

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

 
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

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

const
  url = 'http://www.delphitop.com/';
var
matchs: TMatchCollection;
m: TMatch;
begin
  var http := THttpClient.Create;
  var sl := TStringList.Create;

  try
    sl.LoadFromStream(http.Get(url).ContentStream, TEncoding.ANSI);

matchs := TRegEx.Matches(sl.Text, '<span>(?<price>(\d+[,.])*\d+)</span>');
for m in matchs do
begin
writeln('获取信息: ', m.Groups.Item['price'].Value);
end;

      Readln;
  finally
    sl.Free;
    http.Free;
  end;
end.