public void AddDbAddress(Entity.AddressBase address)
        {
            AddressBase result = Utility.EFContextFactory.GetCurrentDbContext().Set <AddressBase>().Find(address.Code);

            if (result == null)
            {
                DataRepository.DB.Set <AddressBase>().Add(address);
            }
        }
        public int AddAddress(Entity.AddressBase address)
        {
            AddressBase result = Utility.EFContextFactory.GetCurrentDbContext().Set <AddressBase>().Find(address.Code);

            if (result == null)
            {
                return(DataRepository.Add <AddressBase>(address));
            }
            return(-1);
        }
Exemple #3
0
        private static int GetAddress(string url)
        {
            string html = HttpHelper.Get(url);

            if (html == "err")
            {
                System.Threading.Thread.Sleep(1000);
                //GetHtml(url);
                html = HttpHelper.Get(url);
                if (html == "err")
                {
                    throw new Exception();
                }
            }
            if (url.IndexOf("Html/Address") > -1)
            {
                url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016" + url.Substring(url.LastIndexOf("/"));
            }
            Regex           reg     = new Regex("<tr class='(.*?)'>(.*?)</tr>", RegexOptions.IgnoreCase);
            MatchCollection matches = reg.Matches(html);

            foreach (Match match in matches)
            {
                string          entityCode = url.Substring(url.LastIndexOf('/') + 1, url.Length - url.LastIndexOf('/') - 6).PadRight(12, '0');
                Regex           hrefReg    = new Regex("<a href='(.*?)'>");
                Regex           phrefReg   = new Regex("<a href='(.*?)'>(.*?)</a>");
                MatchCollection hrefs      = phrefReg.Matches(match.Value);

                if (match.Value.IndexOf(EnumEntity.AddressType.provincetr.ToString()) > -1)
                {
                    foreach (Match hrefm in hrefs)
                    {
                        //(obj as List<string>).Add(hrefm.Value);
                        AddressBase address = new Entity.AddressBase();
                        string[]    hrefarr = hrefm.Value.Replace("<a href='", "").Replace("'>", ";").Replace("<br/></a>", "").Split(';');
                        address.Code = hrefarr[0].Substring(hrefarr[0].LastIndexOf('/') + 1, hrefarr[0].Length - hrefarr[0].LastIndexOf('/') - 6).PadRight(12, '0');
                        address.Text = hrefarr[1];
                        address.Type = "provincetr";
                        addressLogic.AddDbAddress(address);
                        if (!string.IsNullOrWhiteSpace(hrefm.Value))
                        {
                            GetAddress(url.Substring(0, url.LastIndexOf('/') + 1) + hrefarr[0].Substring(0, 2) + ".html");
                        }
                    }
                }
                else
                {
                    if (hrefs.Count != 0)
                    {
                        string   type    = match.Value.Substring("<tr class='".Length, match.Value.IndexOf("'>") - "<tr class='".Length);
                        string[] hrefarr = new string[2];
                        hrefarr[0] = hrefs[0].Value.Replace("<a href='", "").Replace("'>", ";").Replace("</a>", "").Split(';')[1];
                        hrefarr[1] = hrefs[1].Value.Replace("<a href='", "").Replace("'>", ";").Replace("</a>", "").Split(';')[1];
                        Match       hmatch  = hrefReg.Match(match.Value);
                        AddressBase address = new Entity.AddressBase();
                        address.Code = hrefarr[0];
                        address.Text = hrefarr[1];
                        address.Type = type;
                        addressLogic.AddDbAddress(address);
                        if (!string.IsNullOrWhiteSpace(hmatch.Value) && type != EnumEntity.AddressType.towntr.ToString())
                        {
                            GetAddress(url.Substring(0, url.LastIndexOf('/') + 1) + hmatch.Value.Replace("<a href='", "").ToString().Replace("'>", ""));
                        }
                    }
                }
            }
            int result = addressLogic.SaveDbAddress();

            count += result;
            Log4net.LogHelper.WriteLog("地址统计:" + count.ToString(), url + ";共计:" + result.ToString());

            return(count);
        }