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();
看一下最終效果