标签:res contains ota tco 级别 view 关键词 bnu value
上一篇博文中讲到如何获取用于搜索的关键词,并且已经准备好了一百五十万的关键词 这其中有门牌号码,餐馆酒店名称,公司名称,道路名称等。有了这些数据,我们就可以通过代码,模拟我们在百度地图的搜索框中搜索地点,从而获取其返回的POI数据。下面直接上代码~
一 、准备好用于存储数据的数据库表
由于百度返回的POI数据都是JSON字符串且子节点非常多,为了按照子节点一一存储,就需要建这么一张包含很多字段的表,虽然麻烦,但是信息很全面。当然各位也可以选择性的存储其中的数据。
二、获取并解析数据的代码
class Program { static string[] strArr = { "name", "addr", "address_norm", "alias", "aoi", "area_name", "brand_id", "cla", "di_tag", "ext_display", "geo", "indoor_pano", "navi_update_time", "new_catalog_id", "primary_uid", "show_tag", "std_tag", "storage_src", "street_id", "tag", "tel", "uid" }; static string[] flaArr = { "navi_x", "navi_y" }; static string[] intArr = { "acc_flag", "area", "biz_type", "catalogID", "click_flag", "detail", "diPointX", "diPointY", "dis", "dist2route", "dist2start", "ext_type", "f_flag", "father_son", "flag_type", "geo_type", "ismodified", "pano", "poiType", "poi_click_num", "poi_profile", "prio_flag", "route_flag", "status", "ty", "view_type", "x", "y" }; static List<string> tempList = new List<string>(); static void Main(string[] args) {
//调用SQLHelper类中的方法 DataTable kwDt = SQLHelper.ExecuteDataTable("select KWName from SHKW order by SaveTime", CommandType.Text); List<string> kwList = new List<string>(); foreach (DataRow row in kwDt.Rows) { kwList.Add(row["KWName"].ToString()); } int total = kwList.Count; DataTable tempDt = SQLHelper.ExecuteDataTable("select name,addr from SHPOI", CommandType.Text); foreach (DataRow row in tempDt.Rows) { tempList.Add(row["name"].ToString() + "," + row["addr"].ToString()); } for (int i = 0; i < kwList.Count; i++) { string kw = kwList[i]; string keyWord = "上海市" + kw; for (int j = 0; j < 200; j++) { string url = "http://map.baidu.com/?newmap=1&reqflag=pcmap&biz=1&from=webmap&da_par=direct&pcevaname=pc4.1&qt=s&da_src=searchBox.button&wd=" + keyWord + "&c=289&pn=" + j; int count = DownloadPOIInfoFromBMap(url, -1, keyWord + j); if (count == -1) break; Console.WriteLine("成功写入数据:" + count + "条 执行次数:" + (j + 1) + " KeyWord:" + keyWord + " kwNum:" + (kwList.IndexOf(kw) + 1) + " tatal:" + total); } } Console.WriteLine("ok"); Console.ReadKey(); } private static int DownloadPOIInfoFromBMap(string url, int typeId, string fileName) { int count = 0; Stream responseStream; string restring; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.Default); restring = sr.ReadToEnd(); } catch { Console.WriteLine("网络异常"); return count; } string[] strArr = restring.Split(new string[] { "\\u" }, StringSplitOptions.None); StringBuilder sb = new StringBuilder(); sb.Append(strArr[0]); try { for (int i = 1; i < strArr.Length; i++) { string code = strArr[i]; int value = Convert.ToInt32(code.Substring(0, 4), 16); sb.Append(char.ConvertFromUtf32(value)); if (code.Length > 4) { sb.Append(code.Substring(4, code.Length - 4)); } } } catch { return count; } JObject jo; try { jo = (JObject)JsonConvert.DeserializeObject(sb.ToString()); } catch {
//一些数据无法正确转换为json对象 需要特别处理 Regex regex = new Regex("\"\\{\\\\\"index_tag\\\\\".*?\"\\}\""); //MatchCollection collection = regex.Matches(sb.ToString()); string data = sb.ToString(); data = regex.Replace(data, "\"\""); try { jo = (JObject)JsonConvert.DeserializeObject(data); } catch { Regex regex1 = new Regex("\"\\{\\\\\"shop_id\\\\\".*?\"\\}\""); data = regex1.Replace(data, "\"\""); try { jo = (JObject)JsonConvert.DeserializeObject(data); } catch { try { Console.Write("无法构建JSON对象 "); File.WriteAllText(@"D:\errorData\" + fileName + ".txt", data); } catch { } return count; } } } return SavePOIToDB(jo, typeId); } private static int SavePOIToDB(JObject jo, int typeId) { int count = 0; try { if (jo["content"].Children().Count() == 0) return -1; } catch { if (jo != null) return -1; } foreach (JToken child in jo["content"].Children()) { List<SqlParameter> listParam = new List<SqlParameter>(); foreach (string s in strArr) { listParam.Add(new SqlParameter(@s, GetInfoString(s, child))); } foreach (string f in flaArr) { listParam.Add(new SqlParameter(@f, GetInfoString(f, child))); } foreach (string i in intArr) { listParam.Add(new SqlParameter(@i, GetInfoString(i, child))); } List<float> coordinate = ConvertCoor(GetInfoInt("x", child), GetInfoInt("y", child)); listParam.Add(new SqlParameter(@"TypeId", typeId)); listParam.Add(new SqlParameter(@"lot_bdll", coordinate[0])); listParam.Add(new SqlParameter(@"lat_bdll", coordinate[1])); string tempStr = GetInfoString("name", child).Replace("‘", "") + "," + GetInfoString("addr", child); if (tempList.Contains(tempStr)) continue; tempList.Add(tempStr); string sql = "insert into SHPOI values(@Name,@acc_flag,@addr,@address_norm,@alias,@aoi,@area,@area_name,@biz_type,@brand_id,@catalogID,@cla,@click_flag,@detail,@diPointX,@diPointY,@di_tag,@dis,@dist2route,@dist2start,@ext_display,@ext_type,@f_flag,@father_son,@flag_type,@geo,@geo_type,@indoor_pano,@ismodified,@navi_update_time,@navi_x,@navi_y,@new_catalog_id,@pano,@poiType,@poi_click_num,@poi_profile,@primary_uid,@prio_flag,@route_flag,@show_tag,@status,@std_tag,@storage_src,@street_id,@tag,@tel,@ty,@uid,@view_type,@x,@y,@lot_bdll,@lat_bdll,@TypeId)"; SQLHelper.ExecuteNonQuery(sql, CommandType.Text, listParam.ToArray()); count++; } return count; } private static int GetInfoInt(string key, JToken jo) { try { return Convert.ToInt32(jo[key].ToString()); } catch { return 0; } } private static float GetInfoFloat(string key, JToken jo) { try { return float.Parse(jo[key].ToString()); } catch { return 0; } } private static string GetInfoString(string key, JToken jo) { try { return jo[key].ToString(); } catch { return DBNull.Value.ToString(); } } /// <summary> /// 将bdmac坐标转换城bdll坐标 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> private static List<float> ConvertCoor(int x, int y) { //GevTfxGlAWxzIzTobk7PGX1eu2YF0RMl //ghCXsfOvpXwWsnm6lhWGelF5f0Fh3y82 List<float> list = new List<float>(); string url = "http://api.map.baidu.com/geoconv/v1/?coords=" + x / (100.0) + "," + y / (100.0) + "&from=6&to=5&ak=ghCXsfOvpXwWsnm6lhWGelF5f0Fh3y82"; Stream responseStream; StreamReader sr; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); responseStream = response.GetResponseStream(); sr = new StreamReader(responseStream, Encoding.Default); } catch { list.Add(0); list.Add(0); return list; } string restring = sr.ReadToEnd(); JObject jo; try { jo = (JObject)JsonConvert.DeserializeObject(restring); list.Add(float.Parse(jo["result"][0]["x"].ToString())); list.Add(float.Parse(jo["result"][0]["y"].ToString())); } catch { list.Add(0); list.Add(0); } return list; } }
使用关键词模拟百度地图的搜索功能从而获取返回的POI数据的方法能够获取大量的POI数据 由于返回的数据中没有百度经纬度坐标信息,只有百度墨卡托的坐标,所以还需要调用它的API 通过坐标转换的方方法来获取百度经纬度坐标。这就需要申请开发密钥了,而且每个密钥一天最多只能进行十万次的坐标转换。
以下是本人获取的部分POI数据:
以上就是本人获取百度地图POI数据的方式,由于关键词数量在百万级别 而且百度每次返回的数据的页数也不近相同,多的有百页,少的仅有一条设置没有,所以程序需要运行很长的时间,本人运行两周程序也才获取到一百多万数据。预计程序运行完成时POI数据总量或与关键词总量相差无几,也能达到一百五十万左右,虽然并不知道百度地图上海地区的POI数据具体有多少,但是一百多万的数据量已经能够做些分析了。
标签:res contains ota tco 级别 view 关键词 bnu value
原文地址:http://www.cnblogs.com/yumubaime/p/7258796.html