10分钟采集凡客最新的省、市、区、邮政编码和电话区号(附源码)
生活随笔
收集整理的這篇文章主要介紹了
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";elsethis.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();// 城市idint 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();// 區idint 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";elsethis.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";elsethis.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網站的老數據了,把數據都放進數據庫:
?
?
需要抽取省、市、區數據的朋友可以下載程序源碼來使用:點擊下載
?
轉載于:https://www.cnblogs.com/kudy/archive/2011/12/03/2274945.html
總結
以上是生活随笔為你收集整理的10分钟采集凡客最新的省、市、区、邮政编码和电话区号(附源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bigpipe merge对F5做批量配
- 下一篇: 如何改变X:\Users\XXX的用户名