Last active
February 5, 2025 08:37
-
-
Save donma/29fa383f8a2f344bc256a28e9b71a99d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //call function | |
| //GetSMKData("GH08-9E568"); | |
| /// <summary> | |
| /// Post data to https://www.samakompra.com/searchmycer/SearchCertificate/" | |
| /// | |
| /// </summary> | |
| /// <param name="cardNumber"></param> | |
| private static void GetSMKData(string cardNumber) | |
| { | |
| //基本上就是模擬瀏覽器參數 | |
| var client = new RestClient("https://www.samakompra.com/searchmycer/SearchCertificate/"); | |
| var request = new RestRequest("", Method.Post); | |
| request.AddHeader("accept", "*/*"); | |
| request.AddHeader("accept-language", "zh-TW,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"); | |
| request.AddHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8"); | |
| //要用英文,會比較好閱讀 | |
| request.AddHeader("cookie", "languageUse=en;"); | |
| request.AddHeader("origin", "https://www.samakompra.com"); | |
| request.AddHeader("priority", "u=1, i"); | |
| request.AddHeader("referer", "https://www.samakompra.com/searchmycer/"); | |
| request.AddHeader("sec-ch-ua", "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\""); | |
| request.AddHeader("sec-ch-ua-mobile", "?0"); | |
| request.AddHeader("sec-ch-ua-platform", "\"Windows\""); | |
| request.AddHeader("sec-fetch-dest", "empty"); | |
| request.AddHeader("sec-fetch-mode", "cors"); | |
| request.AddHeader("sec-fetch-site", "same-origin"); | |
| request.AddHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0"); | |
| request.AddHeader("x-requested-with", "XMLHttpRequest"); | |
| //caseType=SearchCertificate , keyword_search=cardNumber | |
| request.AddParameter("application/x-www-form-urlencoded", "caseType=SearchCertificate&keyword_search="+ cardNumber, ParameterType.RequestBody); | |
| var response = client.ExecuteAsync(request).Result; | |
| Console.OutputEncoding = Encoding.UTF8; | |
| //Console.WriteLine(response.Content); | |
| ParseSMKData(response.Content); | |
| } | |
| /// <summary> | |
| /// Parse Post 回來得資料 | |
| /// </summary> | |
| /// <param name="htmlCode"></param> | |
| private static void ParseSMKData(string htmlCode) | |
| { | |
| string html = htmlCode; // 你的 HTML 內容 | |
| // 抓取 img src | |
| string imgPattern = "<img src=\\\"(.*?)\\\""; | |
| string categoryPattern = "<span class=\\\"card-left\\\">Category</span>\\s*<span>(.*?)</span>"; | |
| string namePattern = "<span class=\\\"card-left\\\">Name</span>\\s*<span>(.*?)</span>"; | |
| string placePattern = "<span class=\\\"card-left\\\">Place</span>\\s*<span>(.*?)</span>"; | |
| string provincePattern = "<span class=\\\"card-left\\\">Province</span>\\s*<span>(.*?)</span>"; | |
| string issuedPattern = "<span class=\\\"card-left\\\">Issued</span>\\s*<span>(.*?)</span>"; | |
| string cardNumber = @"<div class=""no-phra"">(.*?)<\/div>"; | |
| Console.OutputEncoding = Encoding.UTF8; | |
| Console.WriteLine("卡號: " + Regex.Match(html, cardNumber).Groups[1].Value); | |
| Console.WriteLine("圖片網址: " + Regex.Match(html, imgPattern).Groups[1].Value); | |
| Console.WriteLine("分類: " + Regex.Match(html, categoryPattern).Groups[1].Value); | |
| Console.WriteLine("名稱: " + Regex.Match(html, namePattern).Groups[1].Value); | |
| Console.WriteLine("地點: " + Regex.Match(html, placePattern).Groups[1].Value); | |
| Console.WriteLine("省分: " + Regex.Match(html, provincePattern).Groups[1].Value); | |
| Console.WriteLine("發卡時間: " + Regex.Match(html, issuedPattern).Groups[1].Value); | |
| } | |
| // Result | |
| /* | |
| 卡號: GH08-9E568 | |
| 圖片網址: https://www.samakompra.com/myceronline/show/images/w4e274a4r5k5z3g434t5k5h253d2m2q295t2n5i4u564d55426s4e574p5e5n2l5o4v554x3o29484q4p4d474a4i5g5k474y33454v2x2v2w3r21423d5w284v2a5r2t4k3140324f4936433e4l2q2n2g4j4f4f2o2b2l4p3d4p2x2t2c4a4z2w2s2o2w266 | |
| 分類: เหรียญพระพุทธ - พระคณาจารย์ | |
| 名稱: เหรียญท่านท้าวมหาพรหมธาดา (เหรียญจักรเพชร) เนื้อทองฝาบาตร พ.ศ. 2508 | |
| 地點: วัดบรมสถลศรีสุทธิโสภณรังสรรค์ (วัดดอน) | |
| 省分: กรุงเทพมหานคร | |
| 發卡時間: 27 August 2024 | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment