之前寫過“使用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類用於儲存資料,程式碼範例:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Update_Train_Code{ /// <summary> /// 網站資訊 /// </summary> public class Station { private string name;//網站名 public string Name { get { return name; } set { name = value; } } private string code;//縮寫 public string Code { get { return code; } set { code = value; } } private string firstLetter;//首字母 public string FirstLetter { get { return firstLetter; } set { firstLetter = value; } } private string pinyin;// 全拼 public string Pinyin { get { return pinyin; } set { pinyin = value; } } private string shorthand;// 簡寫 public string Shorthand { get { return shorthand; } set { shorthand = value; } } private string order;// 排序 public string Order { get { return order; } set { order = value; } } }}
Station網站資訊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()方法中調用的程式碼範例如下:
static void Main(string[] args) { List<Station> lst = new List<Station>(); lst = GetStations(); foreach (Station st in lst) { InsertData(st.Name, st.Code); } Console.WriteLine("已執行完畢!!!"); Console.Read(); }
點擊執行,所有擷取的資訊就這樣輕而易舉更新到了資料庫。。。
以上也算是完整的程式碼範例了,如果您想要源碼,請觸擊以下連結,然後輸入訪問密碼(訪問密碼:83f2):
http://yunpan.cn/QegWkHcdegSF4
一家之言,僅供參考!!!