用CEF4Delphi取网页元素时碰到ElementInnerText里含有" "
比如网页源码里是"内容 "取出来显示就变成"内容?"
搜索大部分是说把" "替换成其它字符即可
但实际操作怎么也替换不了,就算变量为AnsiString也不行
最后用了以下方法解决
参考网页:
https://blog.csdn.net/qq_29683707/article/details/80860904
关于 空格转成正常空格‘ ’的方法
1>先把字符串转码 let data = encodeURI(要转化的值)
2>接下来替换掉 空格 data = data .replace(/%C2%A0/g,'%20'); 3>再转回来就ok了 data = decodeURI(data);
要用到encodeURI,找到Delphi版的
http://www./html/zifuchuan/2599.html
方法1 user Httpapp;
HttpEncode(AnsiToUtf8('中文'));
方法2 uses IdURI;
.. begin S := TIdURI.URLEncode(str); // S := TIdURI.URLDecode(str); end;
使用方法2的TIdURI.URLEncode,程序出错,原因不明
使用方法1成功
直接 TmpTitle:= HTTPEncode(TmpTitle); 即可
但编译器提示警告
[dcc32 Warning] uMiniBrowser.pas(469): W1000 Symbol 'HTTPEncode' is deprecated: 'Use TNetEncoding.URL.Encode' [dcc32 Warning] uMiniBrowser.pas(471): W1000 Symbol 'HTTPDecode' is deprecated: 'Use TNetEncoding.URL.Decode' [dcc32 Hint] uMiniBrowser.pas(469): H2443 Inline function 'HTTPEncode' has not been expanded because unit 'System.NetEncoding' is not specified in USES list [dcc32 Hint] uMiniBrowser.pas(471): H2443 Inline function 'HTTPDecode' has not been expanded because unit 'System.NetEncoding' is not specified in USES list
意思是HTTPEncode和HTTPDecode已经弃用请用TNetEncoding.URL.Encode和TNetEncoding.URL.Decode
点进HTTPEncode看看
function HTTPDecode(const AStr: string): string;
begin
Result := TNetEncoding.URL.Decode(AStr);
end;
function HTTPEncode(const AStr: string): string;
begin
Result := TNetEncoding.URL.Encode(AStr);
end;
其实就直接调用了
TNetEncoding.URL.Encode
所在单元是 System.NetEncoding
|