Firefox Ubuntu linux命令 centos wordpress Python shell linux nginx 程序员 HTML5 微软 google 开源 php Android apache mysql java Windows

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])rnSELECT " + pid.ToString() + ", '" + theP + "'rn";
            else
                this.textBox1.Text += "unionrnSELECT " + pid.ToString() + ", '" + theP + "'rn";
        }));
        // 获取城市数据
        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])rnSELECT " + did.ToString() + ", " + cid.ToString() + ", '" + theD + "', '" + postCode + "'rn";
                    else
                        this.textBox3.Text += "unionrnSELECT " + did.ToString() + ", " + cid.ToString() + ", '" + theD + "', '" + postCode + "'rn";
                }));
            }
            // 增加城市
            this.Invoke(new MethodInvoker(() =>
            {
                if (cid == 1)
                    this.textBox2.Text += "insert into [Db_MoMoMate].[dbo].[City]([Id],[ProvinceId],[Name],[AreaCode])rnSELECT "+ cid.ToString() + ", " + pid.ToString() + ", '" + theC + "', '" + AreaCode + "'rn";
                else
                    this.textBox2.Text += "unionrnSELECT " + cid.ToString() + ", " + pid.ToString() + ", '" + theC + "', '" + AreaCode + "'rn";
            }));
        }
        this.Invoke(new MethodInvoker(() =>
        {
            if (i == provinces.Length - 1)
            {
                this.label1.Text = "done";
                this.button1.Enabled = true;
            }
        }));
    }
}

源码下载:http://q.weibo.com/723895/file/23413872 (新浪微盘)

最终运行效果如下:

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

转自:http://holoblog.iteye.com/blog/1295333

延伸阅读

  • 抱歉,暂无相关内容!

评论