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 ;
             }
         }));
     }
}

 

最终运行效果如下:

 

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值