10分钟采集凡客最新的省、市、区、邮政编码和电话区号(附源码)
最近的开发的项目需要用到省、市、区数据,因为要开发的项目也是电子商务网站,在参考凡客的用户体验时,发现它连深圳最新分离出来的光明新区都有了,拍拍网都没有更新数据,看来凡客在数据更新方面还是挺负责的,所以了解了一下它的数据格式,然后花了点时间写了个小程序来抽取最新的省、市、区、邮政编码和电话区号,邮政编码和电话区号是在用户选择数据后推荐给用户选择的,免得用户再花时间去查,如下界面:
一共有三个地址是返回数据的:
private string GetCityDataUrl( string province) { string url = "http://my.vancl.com/DeliveryAddress/GetCityData?povinceId={0}&r={1}" ; 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网站的老数据了,把数据都放进数据库:
需要抽取省、市、区数据的朋友可以下载程序源码来使用:点击下载
作者:Kudy
出处:http://kudy.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://kudy.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步