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