之前寫過“使用HTTP GET請求12306網站接口獲取車站名和車站Code”。鏈接地址是:
http://www.cnblogs.com/litao4047/archive/2013/05/31/3110781.html
這個鏈接所表述的內容是,從12306網站上利用get解析地址獲取車站名和代號,獲取到的數據就是網站上的那個js文件(數據沒有經過處理),全國火車站代號字典:station_name.js。
今天,所要講述的就是,處理上面鏈接獲取到的數據,寫個插入方法將數據更新到數據庫表中。處理獲取到的數據形式如下:
比如,獲取的一條數據是var station_names ='@bjb|北京北|VAP|beijingbei|bjb|0';經過處理後(字段分割),想要是數據就是'北京北'和'VAP',然後將這樣的數據更新到後台數據庫中。
首先,將形如'@bjb|北京北|VAP|beijingbei|bjb|0'的數據分割成六個字段firstLetter(首字母),name(站點名),code(站點代號),pinyin(全拼),shorthand(縮寫),order(排序),建立一個Model類用於存儲數據,代碼示例:

其次,用get請求http://dynamic.12306.cn/otsweb/js/common/station_name.js地址解析數據,將得到的數據進行緩存,字段分割處理存儲於List<Station>泛型集合中,返回list。
/// <summary>
/// 獲取車站信息
/// </summary>
/// <param name="timeout"></param>
/// <param name="userAgent"></param>
/// <param name="cookie"></param>
public static List<Station> GetStations()
{
string formUrl = "http://dynamic.12306.cn/otsweb/js/common/station_name.js";
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
request.Method = "GET";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.CookieContainer = cookieContainer;
HttpWebResponse SendSMSResponse = (HttpWebResponse)request.GetResponse();
StreamReader SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream());
string response = SendSMSResponseStream.ReadToEnd();
List<Station> list = new List<Station>();
try
{
var str = response;
Regex stationNamesRegex = new Regex("'(?<stationNames>[^\']*?)'");
if (stationNamesRegex.IsMatch(str))
{
string stationNames = stationNamesRegex.Matches(str)[0].Groups["stationNames"].Value;
string[] stations = stationNames.Split('@');
foreach (var station in stations)
{
if (string.IsNullOrEmpty(station))
{
continue;
}
string[] names = station.Split('|');
list.Add(new Station()
{
Shorthand = names[0],
Name = names[1],
Code = names[2],
Pinyin = names[3],
FirstLetter = names[4],
Order = names[5]
});
}
}
}
catch (Exception)
{
}
SendSMSResponse.Close();
SendSMSResponseStream.Close();
return list;
}
再次,連接MySql數據庫,寫一個insert方法,將上述緩存取得的數據更新到數據庫表中。
/// <summary>
/// 向data_jtfw_hc_code表中插入數據
/// </summary>
/// <param name="t_name"></param>
/// <param name="t_code"></param>
public static void InsertData(string t_name, string t_code)
{
using (MySqlConnection conn = new MySqlConnection(MySqlString))
{
string mysql = string.Format("insert into data_jtfw_hc_code(name,code) values ('{0}','{1}')", t_name, t_code);
MySqlCommand cmd = new MySqlCommand(mysql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
}
最後,調用獲取站點信息方法和更新數據庫表數據方法,獲取處理過的信息已經存在了list集合中,通過實例化Model類,根據name,code字段遍歷出數據,往數據庫中更新。。。在Main()方法中調用的代碼示例如下:
Main(<Station> lst = List<Station>= (Station st
點擊執行,所有獲取的信息就這樣輕而易舉的更新到了數據庫。。。
以上也算是完整的代碼示例了,如果您想要源碼,請觸擊以下鏈接,然後輸入訪問密碼(訪問密碼:83f2):
http://yunpan.cn/QegWkHcdegSF4
一家之言,僅供參考!!!