主要城市公交路线下载(可以下载到手机上)
在深圳这种地方,经常坐公交车一坐都是几个小时,不知道还有多长时间到站,去看车顶那牌子又不太方便,所以想要是能放到手机上看就好了
没事就用.net把写了个小软件,可以把深圳(广州 东莞 佛山 上海 北京 武汉 南京 杭州 苏州 天津 沈阳 大连 重庆 成都 西安 郑州 惠州 )等城市的公交线路下载到手机上,这样也能方便的查找路线,毕竟不是所有人都用智能手机上网
关键代码(使用了.net平衡组,看了几个小时才明白了一点):
正则根据class查找div的源码
public static MatchCollection GetDivByClassName(string strClass, string source)
{
Regex reg = new Regex(@"(?is)<div(?:(?!class=).)*class=(['""]?)" + strClass + @"\1[^>]*>(?><div[^>]*>(?<Open>)|</div>(?<-Open>)|(?:(?!</?div\b).)*)*(?(Open)(?!))</div>");
return reg.Matches(source);
}
{
Regex reg = new Regex(@"(?is)<div(?:(?!class=).)*class=(['""]?)" + strClass + @"\1[^>]*>(?><div[^>]*>(?<Open>)|</div>(?<-Open>)|(?:(?!</?div\b).)*)*(?(Open)(?!))</div>");
return reg.Matches(source);
}
下载的代码如下:
首先下载页面的html源代码,然后查找这个页面的所有公交路线
然后找路线的名字、票价、具体站点、是否有备注,
点击下载按钮代码
try
{
string strFormat = txtURLFormat.Text, strHTML, strBusNum, strServiceInfo, strRoute, strPs, strSavePath, strSN = comboBox1.Items.Count > 0 ? dt.Rows[comboBox1.SelectedIndex]["sn"].ToString() : "sz";
int iStart = int.Parse(txtStart.Text), iEnd = int.Parse(txtEnd.Text);
StringBuilder sbResult = new StringBuilder();
Regex regBusNum = new Regex("(?is)<a(?>[^>]+)>(?<busNum>[^<]*)</a>", RegexOptions.Compiled);
MatchCollection matItems, matDetail;
WebClient webDownloader = new WebClient();
Thread thread = new Thread(new ThreadStart(delegate()
{
for (int i = iStart; i <= iEnd; i++)
{
btnDownload.Text = string.Format("正在下载({0}/{1})...", i - iStart + 1, iEnd - iStart + 1);
try
{
strHTML = webDownloader.DownloadString(string.Format(strFormat, i));//获取网页源文件
}
catch (Exception ee) { ShowException(ee); return; }
strHTML = RegexHelper.GetDivByClassName("w720 fleft", strHTML)[0].Value;//所有路线的父容器,因为下面有个class为border的div
matItems = RegexHelper.GetDivByClassName(strSN=="sz"?"border":"bd_ddd", strHTML);//获取 路线 ,深圳的跟其它的不一样
foreach (Match item in matItems)
{
strHTML = item.Value;
strBusNum = regBusNum.Match(strHTML).Groups["busNum"].Value;//公交车次
matDetail = RegexHelper.GetDivByClassName("pad8lr lh24", strHTML);
strServiceInfo = RegexHelper.RemoveWhitespace(RegexHelper.RemoveMarkup(matDetail[0].Value), " ");//运营时间和票价
strRoute = RegexHelper.RemoveMarkup(matDetail[1].Value);//具体路线
strPs = RegexHelper.RemoveWhitespace(RegexHelper.RemoveMarkup(matDetail[3].Value.IndexOf("备注") != -1 ? matDetail[3].Value : string.Empty));//备注就获取
sbResult.AppendFormat("\n{0}\n{1}\n{2}\n{3}", strBusNum, strServiceInfo, strRoute, strPs==string.Empty?string.Empty:strPs+"\n");
}
}
btnDownload.Text = string.Format("下载已完成!");
strSavePath = Path.Combine(Application.StartupPath, string.Format("bus_route_{0}.txt",strSN));
StreamWriter sw = new StreamWriter(strSavePath, false, Encoding.Default);
sw.WriteLine(sbResult.ToString());
sw.Close();
sbResult.Insert(0, string.Format("(文件已经保存在:{0})\n\n", strSavePath));
rtbResult.Text = sbResult.ToString();
}));
thread.IsBackground = true;
thread.Start();
}
catch (Exception ex)
{
ShowException(ex);
}
{
string strFormat = txtURLFormat.Text, strHTML, strBusNum, strServiceInfo, strRoute, strPs, strSavePath, strSN = comboBox1.Items.Count > 0 ? dt.Rows[comboBox1.SelectedIndex]["sn"].ToString() : "sz";
int iStart = int.Parse(txtStart.Text), iEnd = int.Parse(txtEnd.Text);
StringBuilder sbResult = new StringBuilder();
Regex regBusNum = new Regex("(?is)<a(?>[^>]+)>(?<busNum>[^<]*)</a>", RegexOptions.Compiled);
MatchCollection matItems, matDetail;
WebClient webDownloader = new WebClient();
Thread thread = new Thread(new ThreadStart(delegate()
{
for (int i = iStart; i <= iEnd; i++)
{
btnDownload.Text = string.Format("正在下载({0}/{1})...", i - iStart + 1, iEnd - iStart + 1);
try
{
strHTML = webDownloader.DownloadString(string.Format(strFormat, i));//获取网页源文件
}
catch (Exception ee) { ShowException(ee); return; }
strHTML = RegexHelper.GetDivByClassName("w720 fleft", strHTML)[0].Value;//所有路线的父容器,因为下面有个class为border的div
matItems = RegexHelper.GetDivByClassName(strSN=="sz"?"border":"bd_ddd", strHTML);//获取 路线 ,深圳的跟其它的不一样
foreach (Match item in matItems)
{
strHTML = item.Value;
strBusNum = regBusNum.Match(strHTML).Groups["busNum"].Value;//公交车次
matDetail = RegexHelper.GetDivByClassName("pad8lr lh24", strHTML);
strServiceInfo = RegexHelper.RemoveWhitespace(RegexHelper.RemoveMarkup(matDetail[0].Value), " ");//运营时间和票价
strRoute = RegexHelper.RemoveMarkup(matDetail[1].Value);//具体路线
strPs = RegexHelper.RemoveWhitespace(RegexHelper.RemoveMarkup(matDetail[3].Value.IndexOf("备注") != -1 ? matDetail[3].Value : string.Empty));//备注就获取
sbResult.AppendFormat("\n{0}\n{1}\n{2}\n{3}", strBusNum, strServiceInfo, strRoute, strPs==string.Empty?string.Empty:strPs+"\n");
}
}
btnDownload.Text = string.Format("下载已完成!");
strSavePath = Path.Combine(Application.StartupPath, string.Format("bus_route_{0}.txt",strSN));
StreamWriter sw = new StreamWriter(strSavePath, false, Encoding.Default);
sw.WriteLine(sbResult.ToString());
sw.Close();
sbResult.Insert(0, string.Format("(文件已经保存在:{0})\n\n", strSavePath));
rtbResult.Text = sbResult.ToString();
}));
thread.IsBackground = true;
thread.Start();
}
catch (Exception ex)
{
ShowException(ex);
}
BusRouteSearch.rar下载看看 (数据由本地宝网站提供,在此感谢)