示例#1
0
        /// <summary>
        /// 生成JSON区域数据
        /// 1~3级区域
        /// </summary>
        /// <param name="level"></param>
        public static void GenerateJsonArea(int level = 3)
        {
            if (level < 1 || level > 3)
            {
                Console.WriteLine("Error Level");
                return;
            }
            List <RegionJsonModel> areaList;

            using (var dbContext = new RegionDbContext())
            {
                var areaStatus = new List <int> {
                    1, 2, 3
                };
                var query = dbContext.Region.AsNoTracking()
                            .Where(a => areaStatus.Contains(a.Status) && a.IsDel == false)
                            .Select(a => new RegionJsonModel
                {
                    Code       = a.Code,
                    Name       = a.Name,
                    ParentCode = a.ParentCode,
                    Level      = a.Level
                }).OrderBy(a => a.ParentCode).ThenBy(a => a.Code);

                areaList = query.Where(a => a.Level == 1).ToList();
                if (level > 1)
                {
                    foreach (var area in areaList)
                    {
                        area.Children = query.Where(a => a.ParentCode == area.Code).ToList();
                        if (level == 2)
                        {
                            continue;
                        }

                        foreach (var areaChild in area.Children)
                        {
                            areaChild.Children = query.Where(a => a.ParentCode == areaChild.Code).ToList();
                        }
                    }
                }
            }

            var          currentPath  = Environment.CurrentDirectory;
            const string projectName  = "NationRegion";
            var          projectIndex = currentPath.IndexOf(projectName, StringComparison.OrdinalIgnoreCase);
            var          jsonPath     = Path.Combine(currentPath.Substring(0, projectIndex), projectName + "\\Data\\JsonRegion.json");

            var setting = new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };
            var jsonAreaListStr = JsonConvert.SerializeObject(areaList, setting);

            File.WriteAllText(jsonPath, jsonAreaListStr, Encoding.UTF8);
        }
示例#2
0
        /// <summary>
        /// 省市
        /// </summary>
        private static void GetProvince(string regionBaseUrl)
        {
            Console.WriteLine("省级获取开始");
            //HtmlWeb htmlWeb = new HtmlWeb();
            //htmlWeb.OverrideEncoding = Encoding.GetEncoding("GB2312");
            //var doc = htmlWeb.Load(regionBaseUrl);

            var htmlContent = WebHtmlHelper.GetHtmlContent(regionBaseUrl);
            var htmlDoc     = new HtmlDocument();

            htmlDoc.LoadHtml(htmlContent);

            var provinceTrs = htmlDoc.DocumentNode.SelectNodes("//tr[@class='provincetr']");
            var regionList  = new List <Region>();

            foreach (var provinceTr in provinceTrs)
            {
                var provinceTdNodes = provinceTr.SelectNodes("td");
                foreach (var provinceTd in provinceTdNodes)
                {
                    var aNodes = provinceTd.SelectNodes("a");
                    if (aNodes == null || aNodes.Count == 0)
                    {
                        continue;
                    }
                    var provinceNode = aNodes.First();
                    var href         = provinceNode.Attributes["href"].Value;
                    var code         = GetCodeByHref(href);
                    if (string.IsNullOrEmpty(code))
                    {
                        continue;
                    }
                    var name = provinceNode.InnerText;

                    var region = new Region
                    {
                        Name         = name.Trim(),
                        Code         = code,
                        ParentCode   = string.Empty,
                        Level        = 1,
                        ChildNodeUrl = GetHrefFullUrl(regionBaseUrl, href),
                        Status       = 1 // 使用
                    };
                    regionList.Add(region);
                }
            }
            //710000     台湾省
            //810000     香港特别行政区
            //820000     澳门特别行政区
            regionList.Add(new Region
            {
                Name       = "台湾省",
                Code       = "71",
                ParentCode = string.Empty,
                Level      = 1,
                Status     = 1 // 使用
            });
            regionList.Add(new Region
            {
                Name       = "香港特别行政区",
                Code       = "81",
                ParentCode = string.Empty,
                Level      = 1,
                Status     = 1 // 使用
            });
            regionList.Add(new Region
            {
                Name       = "澳门特别行政区",
                Code       = "82",
                ParentCode = string.Empty,
                Level      = 1,
                Status     = 1 // 使用
            });
            var i = 0;

            using (var dbContext = new RegionDbContext())
            {
                regionList = regionList.OrderBy(r => r.Code).ToList();
                foreach (var region in regionList)
                {
                    if (dbContext.Region.Any(a => a.Code == region.Code))
                    {
                        Console.WriteLine($"Code:{region.Code},Name:{region.Name} 已存在");
                        continue;
                    }
                    dbContext.Region.Add(region);
                    i++;
                }
                dbContext.SaveChanges();
            }
            Console.WriteLine($"省级获取完毕:共 {regionList.Count}条,添加 {i}条");
            GetCitys(regionList);
        }
示例#3
0
        /// <summary>
        /// 获取子级区域
        /// </summary>
        /// <param name="parentregionList"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        private static List <Region> GetRegions(List <Region> parentregionList, string className)
        {
            if (!parentregionList.Any())
            {
                return(new List <Region>());
            }
            var random     = new Random();
            var regionList = new List <Region>();
            var addedCount = 0;

            foreach (var parentRegion in parentregionList)
            {
                if (string.IsNullOrEmpty(parentRegion.ChildNodeUrl))
                {
                    continue;
                }

                var htmlContent = WebHtmlHelper.GetHtmlContent(parentRegion.ChildNodeUrl);
                var htmlDoc     = new HtmlDocument();
                htmlDoc.LoadHtml(htmlContent);

                var regionTrs      = htmlDoc.DocumentNode.SelectNodes("//tr[@class='" + className + "']");
                var tempregionList = new List <Region>();
                if (regionTrs == null)
                {
                    var currentRegion = new Region
                    {
                        ParentCode   = parentRegion.Code,
                        Level        = parentRegion.Level + 1,
                        Name         = parentRegion.Name,
                        Code         = parentRegion.Code + "00",
                        ChildNodeUrl = parentRegion.ChildNodeUrl,
                        Status       = 3 //没有该级区域,手动维护的
                    };
                    if (tempregionList.Any(a => a.Code == currentRegion.Code))
                    {
                        continue;
                    }
                    tempregionList.Add(currentRegion);
                }
                else
                {
                    foreach (var regionTr in regionTrs)
                    {
                        var regionTds     = regionTr.SelectNodes("td");
                        var regions       = regionTds[1].SelectNodes("a");
                        var currentRegion = new Region
                        {
                            ParentCode = parentRegion.Code,
                            Level      = parentRegion.Level + 1,
                            Status     = 1
                        };
                        var name = regionTds[1].InnerText.Trim();
                        if (regions == null)
                        {
                            var aCode = regionTds[0].InnerText.TrimEnd('0');
                            if (aCode.Length % 2 != 0)
                            {
                                aCode += "0";
                            }
                            currentRegion.Name   = name;
                            currentRegion.Code   = aCode;
                            currentRegion.Status = 0;//不使用
                            tempregionList.Add(currentRegion);
                            continue;
                        }

                        var region = regions[0];
                        name = region.InnerText.Trim();
                        if (name == "市辖区")
                        {
                            name = parentRegion.Name;
                            currentRegion.Status = 2;//改名了,直辖市 的二级区域 从市辖区 改为父级名称
                        }
                        var href = region.Attributes["href"].Value;
                        var code = GetCodeByHref(href);

                        currentRegion.Name         = name;
                        currentRegion.Code         = code;
                        currentRegion.ChildNodeUrl = GetHrefFullUrl(parentRegion.ChildNodeUrl, href);
                        if (tempregionList.Any(a => a.Code == currentRegion.Code))
                        {
                            continue;
                        }
                        tempregionList.Add(currentRegion);
                    }
                }

                using (var dbContext = new RegionDbContext())
                {
                    tempregionList = tempregionList.OrderBy(r => r.Code).ToList();
                    foreach (var region in tempregionList)
                    {
                        if (dbContext.Region.Any(a => a.Code == region.Code))
                        {
                            Console.WriteLine($"Code:{region.Code},Name:{region.Name} 已存在");
                            continue;
                        }
                        dbContext.Region.Add(region);
                        addedCount++;
                    }

                    parentRegion.IsGetChild = true;
                    dbContext.Region.Update(parentRegion);
                    dbContext.SaveChanges();
                }
                regionList.AddRange(tempregionList);
                var sleepTime = random.Next(500, 800) + parentRegion.Level * 60;
                Thread.Sleep(sleepTime);
            }
            Console.WriteLine($"共 {regionList.Count}条,添加 {addedCount}条");
            return(regionList);
        }
 public RegionController()
 {
     context = new RegionDbContext();
 }