使用C#语言破解58字体反爬
58简历进行时抓取时,抓取电话或一些名字都是加密过,不能明文展示。
网上已经有很多解决方案。但使用C#实现的很少,就当记录一下。
第一步获取base64
var content = await page.GetContentAsync(); var pattern = @"charset=utf-8\;base64(.*?)\)"; var base64 = Regex.Matches(content, pattern); var str = base64[1].Value.Substring(base64[1].Value.IndexOf(',') + 1).Replace(")", "");
第二步 将base64转换成XML.
C#找了一圈没找到好用的库,最后使用python的 fonttools实现,必须要安装python环境,比较麻烦。有知道C#的处理也麻烦告之一下。python环境搭建跟fonttools 安装就不介绍,网上很多。先写一段python的脚本.
import base64
import io
import sys
from fontTools.ttLib import TTFont
def xml_file(base64_str,path):
base64_bin = base64.b64decode(base64_str)
font = TTFont(io.BytesIO(base64_bin))
font.saveXML(path)
if __name__ == '__main__':
xml_file(sys.argv[1],sys.argv[2])
然后在C#使用控制台执行脚本就可以了。执行完可以看到输出的XML文件
string[] strarr = new string[2]; strarr[0] = para;//获取base64 strarr[1] = @"C:\\58resum1.xml"; Process p = new Process(); string path = @"C:\\PythonScripts\resume.py"; string sArguments = path; var pythonPath = System.Configuration.ConfigurationManager.AppSettings["pythonPath"]; p.StartInfo.FileName = pythonPath; foreach (var st in strarr) { sArguments += " " + st; } sArguments += " " + "-u"; p.StartInfo.Arguments = sArguments; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.WaitForExit();
到了这一步基本已完成了,剩下就是解析XML然后找到对应关系。下面这串代码是将58抓取的字体转换成十六进,然后去XML找到对应关系就好了。
var uncode = ageText.Select(t => string.Format(@"{0:X4}", Convert.ToUInt16(t))).ToList();
看一下最终效果