【发布时间】:2014-08-25 19:36:55
【问题描述】:
我正在尝试将此 c# 类移植到 Delphi: http://lab.abhinayrathore.com/imdb/imdb_asp_csharp.htm
我已经编写了 Helper 类,例如 MAtchAll 等,并编写了从网页获取 http 的代码。
但我的问题是这一行:
ArrayList imdbUrls = matchAll(@"<a href=""(http://www.imdb.com/title/tt\d{7}/)"".*?>.*?</a>", html);
这是我的 MatchAll 版本
function MatchAll(const aExpression, aHtml: string; i: Integer = 0): TStringDynArray;
var
ResultArray: TStringDynArray;
Match: TMatch;
begin
SetLength(ResultArray, 0);
for Match in TRegEx.Matches(aHtml, aExpression, [roMultiLine]) do
begin
SetLength(ResultArray, length(ResultArray) + 1);
ResultArray[length(ResultArray) - 1] := Match.Groups[i].Value.Trim;
end;
Result := ResultArray;
end;
还有我的 GetUrlData 版本
function RandomNext(const AFrom, ATo: Integer): string;
begin
Result := IntToStr(Random(ATo - AFrom) + AFrom);
end;
function GetUrlData(const Url: string): string;
var
Client: TIdHTTP;
begin
Client := TIdHTTP.Create(nil);
// Random IP Address
Client.Request.CustomHeaders.AddValue('X-Forwarded-For', Format('%d.%d.%d.%d', [Random(255), Random(255), Random(255), Random(255)]));
// Random User-Agent
Client.Request.CustomHeaders.AddValue('User-Agent', 'Mozilla/' + RandomNext(3, 5) + '.0 (Windows NT ' + RandomNext(3, 5) + '.' + RandomNext(0, 2) + '; rv:2.0.1) Gecko/20100101 Firefox/' +
RandomNext(3, 5) + '.' + RandomNext(0, 5) + '.' + RandomNext(0, 5));
Result := Client.Get(Url);
FreeAndNil(Client);
end;
鉴于此网址:http://www.google.com/search?q=imdb+13%20sins
如何提取 IMDB 网址?
延斯·博瑞肖特
【问题讨论】:
-
转义这部分的点,如
http://www\.imdb\.com/title -
感谢您的回答 这成功了:www\.imdb\.com/title/tt\d{7}
-
如果我是你,我会使用
the IMDB API。