欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

10分钟采集凡客最新的省、市、区、邮政编码和电话区号(附源码)  

程序员文章站 2022-05-06 16:17:08
...

最近的开发的项目需要用到省、市、区数据,因为要开发的项目也是电子商务网站,在参考凡客的用户体验时,发现它连深圳最新分离出来的光明新区都有了,拍拍网都没有更新数据,看来凡客在数据更新方面还是挺负责的,所以了解了一下它的数据格式,然后花了点时间写了个小程序来抽取最新的省、市、区、邮政编码和电话区号,邮政编码和电话区号是在用户选择数据后推荐给用户选择的,免得用户再花时间去查,如下界面: 

10分钟采集凡客最新的省、市、区、邮政编码和电话区号(附源码) 
            
    
    
         

 

一共有三个地址是返回数据的:

private string GetCityDataUrl(string province)
{
 
    return string.Format(url, JScriptUtil.Escape(province), GetRandom());
}
 
private string GetDistrictDataUrl(string province, string city)
{
    string url = "http://my.vancl.com/DeliveryAddress/GetAreaData?cityId={0}&povinceId={1}&r={2}";
 
    return string.Format(url, JScriptUtil.Escape(city), JScriptUtil.Escape(province), GetRandom());
}
 
private string GetPostCodeUrl(string province, string city, string district)
{
    string url = "http://my.vancl.com/DeliveryAddress/GetPostalCode?povince={0}&city={1}&area={2}&r={3}";
 
    return string.Format(url, Utils.UrlEncode(province), Utils.UrlEncode(city), Utils.UrlEncode(district), GetRandom());
}

 

核心函数:

// 抽取凡客数据
private void DoGetVanclData()
{
    string[] provinces = Util.Provinces.Split('|');
    for (int i = 0; i < provinces.Length; i++)
    {
        string theP = provinces[i].Trim();
 
        this.Invoke(new MethodInvoker(() =>
        {
            this.label1.Text = theP + "...";
        }));
 
        // 增加省份
        int pid = GetNewId("Province");
        this.Invoke(new MethodInvoker(() =>
        {
            if (pid == 1)
                this.textBox1.Text += "insert into [Db_MoMoMate].[dbo].[Province]([Id],[Name])\r\nSELECT " + pid.ToString() + ", '" + theP + "'\r\n";
            else
                this.textBox1.Text += "union\r\nSELECT " + pid.ToString() + ", '" + theP + "'\r\n";
        }));
 
        // 获取城市数据
        string[] cityData = WebRequestHelper.HttpGet(GetCityDataUrl(theP), "", Encoding.UTF8).Split('$');
        Thread.Sleep(10);
 
        foreach (string city in cityData)
        {
            string theC = city.Split(',')[1].Trim();
 
            // 城市id
            int cid = GetNewId("City");
            string AreaCode = null;
 
            // 获取区数据
            string[] districtData = WebRequestHelper.HttpGet(GetDistrictDataUrl(theP, theC), "", Encoding.UTF8).Split('$');
            Thread.Sleep(10);
 
            foreach (string district in districtData)
            {
                string theD = district.Split(',')[1].Trim();
 
                // 区id
                int did = GetNewId("District");
                string[] postData = WebRequestHelper.HttpGet(GetPostCodeUrl(theP, theC, theD), "", Encoding.UTF8).Split('$');
 
                if (AreaCode == null)
                {
                    AreaCode = postData[4];
                }
 
                string postCode = postData[3];
 
                Thread.Sleep(10);
                // 增加区
                this.Invoke(new MethodInvoker(() =>
                {
                    if (did == 1)
                        this.textBox3.Text += "insert into [Db_MoMoMate].[dbo].[District]([Id],[CityId],[Name],[PostCode])\r\nSELECT " + did.ToString() + ", " + cid.ToString() + ", '" + theD + "', '" + postCode + "'\r\n";
                    else
                        this.textBox3.Text += "union\r\nSELECT " + did.ToString() + ", " + cid.ToString() + ", '" + theD + "', '" + postCode + "'\r\n";
                }));
            }
 
            // 增加城市
            this.Invoke(new MethodInvoker(() =>
            {
                if (cid == 1)
                    this.textBox2.Text += "insert into [Db_MoMoMate].[dbo].[City]([Id],[ProvinceId],[Name],[AreaCode])\r\nSELECT " + cid.ToString() + ", " + pid.ToString() + ", '" + theC + "', '" + AreaCode + "'\r\n";
                else
                    this.textBox2.Text += "union\r\nSELECT " + cid.ToString() + ", " + pid.ToString() + ", '" + theC + "', '" + AreaCode + "'\r\n";
            }));
        }
 
        this.Invoke(new MethodInvoker(() =>
        {
            if (i == provinces.Length - 1)
            {
                this.label1.Text = "done";
                this.button1.Enabled = true;
            }
        }));
    }
}

 

最终运行效果如下:

10分钟采集凡客最新的省、市、区、邮政编码和电话区号(附源码) 
            
    
    
         

 

本公司项目中还需要大学数据,那是09年开发大学SNS网站的老数据了,把数据都放进数据库:

10分钟采集凡客最新的省、市、区、邮政编码和电话区号(附源码) 
            
    
    
         

 

10分钟采集凡客最新的省、市、区、邮政编码和电话区号(附源码)