当前位置: 首页 > 技术文档 > 正文

提取网址: url转码 idhttp下载中文的网址是会404的 这个函数把中文部分转下码就可以用idhttp下 […]

提取网址:

function qianurl(URL: String): String;
begin
  if Pos('http://', URL) > 0 then Delete(Url, 1, 7);
  Result := Copy(Url, 1, Pos('/', Url) - 1);

  if Pos('https://', URL) > 0 then Delete(Url, 1, 8);
  Result := Copy(Url, 1, Pos('/', Url) - 1);

  if Result = '' then Result := URL;
end;

url转码 idhttp下载中文的网址是会404的 这个函数把中文部分转下码就可以用idhttp下载了,uses 需要加入 httpapp.

function urlencode(Aurl: string): string;
var
  i: integer;
  stmp,Tstmp: string;
begin
  result:=Aurl;
  if length(Aurl) > 0 then
  begin
    for i := 1 to length(Aurl) do
    begin
      if Integer(Ord(Aurl[i])) >255 then
        begin
        stmp := copy(Aurl, i, 1);
        Tstmp:=HttpEncode(stmp);
        result:=stringreplace(result,stmp,Tstmp,[]);
        end;
    end;
  end;
end;

取url的文件名,实际就是取最后一个”/”后面的数据!

function GetName(aURL: string): string;
var
  i: integer;
  s: string;
begin

  s := aURL;
  i := Pos('/', s);
  while i <> 0 do
  begin
    Delete(s, 1, i);
    i := Pos('/', s);
  end;
  Result := s;
end;

提取网页中的图片及js链接, ‘src=”(.*?)‘  红色部分是从什么字符匹配到什么字符,

function tiqu_src(const AInputString: string): utf8string;
const
  tiqu = 'src="(.*?)"' ;
var
  r: TRegExpr;
begin
  Result := '';
  r := TRegExpr.Create;
  try
    r.Expression := tiqu;
    if r.Exec(AInputString) then
    repeat
      Result := Result + utf8string(r.Match[0]) + #13#10;
    until not r.ExecNext;
  finally

    r.Free;
  end;
end;
function tiqu_href(const AInputString: string): utf8string;
const

  tiqu = 'href="(.*?)"' ;
var
  r: TRegExpr;
begin
  Result := '';
  r := TRegExpr.Create;
  try
    r.Expression := tiqu;
    if r.Exec(AInputString) then
    repeat
      Result := Result +utf8string(r.Match[0]) + #13#10;
    until not r.ExecNext;
  finally

    r.Free;
  end;
end;
本文固定链接: https://blog.meyisi.cn/jishu/949.html | 么意思博客
标签:

delphi 提取网页及地址相关函数:等您坐沙发呢!

发表评论

快捷键:Ctrl+Enter