全国各地号码段采集工具

话不多说,先上图  最新在写一个项目,需要通过IP生成IP所在地对应的号码,大体的业务逻辑就是根据IP获取到对应的城市,根据城市找到号码段,然后生成随机号码,代码比较乱,仅供参考!
static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("========================================华少号码段维护工具=================================");
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "phone.dic");
            if (File.Exists(filePath))
            {
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("字典存在已经存在,如需重建,请删除Phone.dic然后重新打开程序!");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("字典文件不存在,回复Y建立字典(不区分大小写):");
                var key = Console.ReadLine();
                if (key.ToLower() == "y")
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine("建立中,建立完成后需要重启网站才可运用最新字典,时间较长,请耐心等待!");
                    Dictionary<string, HashSet<string>> directory = new Dictionary<string, HashSet<string>>();
                    var http = new HttpClient();
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    var html = new HttpHelper().GetHtml(new HttpItem { URL = "http://www.hiphop8.com/all.html" }).Html;
                    Console.Write("正在获取城市数据.......");
                    var matchResult = Regex.Matches(html, "<LI><A href="(http://www.hiphop8.com/city/[\s\S]+?\.php)" target=_blank>([^<]+?)</A>[\s\S]+?</LI>");
                    Console.WriteLine("共获取到" + matchResult.Count + "个城市,准备获取号码段......");
                    for (int i = 0; i < matchResult.Count; i++)
                    {
                        var cityMatch = matchResult[i];
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        var city = cityMatch.Groups[2].Value;
                        var url = cityMatch.Groups[1].Value;
                        Console.Write($"正在获取【{city}】的号码段数据>>>>>>>>>>");
                        try
                        {
                            var cityHtml = http.GetStringAsync(url).Result;
                            var cityMatchs = Regex.Matches(cityHtml, "(\d+?).html");
                            var numberSet = new HashSet<string>();
                            foreach (Match item in cityMatchs)
                            {
                                numberSet.Add(item.Groups[1].Value);
                            }
                            directory[city + "市"] = numberSet;
                            Console.Write($"【{city}】号码段获取完成,共获取到【{numberSet.Count}】个号码段>>>>>>>>>>");

                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write($"Error:【{city}】号码段获取失败【{ex.Message}】>>>>>>>>>>");
                        }
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine($"当前【{i+1}】---总共【{matchResult.Count}】,休眠5秒");
                        Thread.Sleep(5000);
                    }
                    var jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(directory);
                    File.WriteAllText(filePath,jsonStr);
                    Console.WriteLine("获取完成,保存成功!");
                }
                else
                {
                    Console.WriteLine("用户取消!");
                }
            }
            Console.ReadLine();
        }
 刚开始准备用AngleSharp来解析HTML获取号码段的,后来发现还是正则好使!本人一直专注于营销软件开发,欢迎有共同爱好的朋友加好友一起交流。 

相关内容推荐