public List<SmartLaw.Model.BusLine> BusLineFilterByName(DataTable ds,string value) { Regex r0 = new Regex("^[^0-9]*?" + value + "[^0-9]*?"); Regex r1=new Regex("[0-9]+?"+value); Regex r2 = new Regex(value + "[0-9]+?"); List<Model.BusLine> models=new List<Model.BusLine>(); //DataTable ds2 = new DataTable(); for (int i = 0; i < ds.Rows.Count; ++i) { string s=r0.Match(ds.Rows[i]["RouteName"].ToString()).Value; if (s != null&& s != "") { if (r1.Match(ds.Rows[i]["RouteName"].ToString()).Value == "" && r2.Match(ds.Rows[i]["RouteName"].ToString()).Value == "") { Model.BusLine amodel = new Model.BusLine(); amodel = dal.DataRowToModel(ds.Rows[i]); models.Add(amodel); } } } return models; }
private Model.BusLine GetBusLineInfo(string infoURL) { Model.BusLine blModel = new Model.BusLine(); string str = PostWebRequest(infoURL, "", encoding, false); Regex qzp = new Regex("<span class=\"red mr10\">.+?</span>站牌信息"); Regex qzp0 = new Regex("<span class=\"red mr10\">"); Regex qzp1 = new Regex("</span>站牌信息"); string routName = qzp.Match(str).Value; routName= qzp0.Replace(routName, ""); routName = qzp1.Replace(routName, ""); blModel.RouteName = routName; Regex qrm = new Regex("<div class=\"line_info\">[\\s\\S]+?<div class=\"line_point\">"); Regex qrm0 = new Regex("<p>.+?</p>"); Regex qrm1 = new Regex("<p>"); Regex qrm2 = new Regex("</p>"); string remarks = ""; remarks= qrm.Match(str).Value; remarks = qrm0.Match(remarks).Value; remarks = qrm1.Replace(remarks, ""); remarks = qrm2.Replace(remarks, ""); Regex qrmk = new Regex("<div class=\"line_time\">[\\s\\S]+?<div class=\"line_point\">"); Regex qrmk0 = new Regex("<p>.+?</p>"); Regex qrmk1 = new Regex("<p>"); Regex qrmk2 = new Regex("</p>"); string remarks2 = ""; remarks2 = qrmk.Match(str).Value; remarks2 = qrmk0.Match(remarks2).Value; remarks2 = qrmk1.Replace(remarks2, ""); remarks2 = qrmk2.Replace(remarks2, ""); remarks = remarks + remarks2; blModel.Remarks = remarks; List<string> stations = new List<string>(); Regex qstt = new Regex("<a href=\"http://bus.aibang.com/ningbo/station-[\\s\\S]+? name=\".+?\""); Regex qstt0 = new Regex("<a href=\"http://bus.aibang.com/ningbo/station-[\\s\\S]+? name=\""); Regex qstt1 = new Regex("\""); MatchCollection mCollection = qstt.Matches(str); foreach (Match m in mCollection) { string astt = qstt0.Replace(m.ToString(), ""); astt = qstt1.Replace(astt, ""); stations.Add(astt); } blModel.Station = stations.ToArray(); return blModel; }
public bool GetBusLine() { string htm = PostWebRequest(url,"", encoding, false); List<string> pageURLs= GetPageURLs(htm);//获取每一页的url pageURLs.Add(url); string msg = ""; for (int i = 0; i < pageURLs.Count; ++i)//对于每一页 { string pageURL = ""; if (i != pageURLs.Count - 1) { pageURL= "http://bus.aibang.com" + pageURLs[i]; } else { pageURL = pageURLs[i]; } string urls=PostWebRequest(pageURL,"",encoding,false); List<string> infoURLs = GetInfoURLs(urls);//获取每一页上公交线路信息的url for (int j = 0; j < infoURLs.Count; ++j)//对于每个线路 { Model.BusLine model = new Model.BusLine(); model = GetBusLineInfo(infoURLs[j]); try { dal.AddBusLine(model, out msg); } catch { return false; } } } if (msg != "") { return false; } else { return true; } }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.BusLine DataRowToModel(DataRow row) { Model.BusLine model = new Model.BusLine(); if (row != null) { if (row["AutoID"] != null && row["AutoID"].ToString() != "") { model.AutoID = long.Parse(row["AutoID"].ToString()); } if (row["RouteName"] != null) { model.RouteName = row["RouteName"].ToString(); } if (row["Station"] != null && row["Station"].ToString() != "") { model.Station = row["Station"].ToString().Split('|'); } if (row["Remarks"] != null) { model.Remarks = row["Remarks"].ToString(); } } return model; }