C#获取网页数据的两个函数
2014-12-15 09:38
453 查看
[b]1.获取网页源码函数[/b]
[b]2.处理所获得的源码[/b]
注:HtmlDocument、HtmlNode、HtmlNodeCollection三个类需要引用一个类库文件。该文件在博客园的后台文件管理中。
string getPageText(string url) { string retVal = ""; dtUpdate = DateTime.Now; labMessage.Text = ""; int pageNo = 1; string strUrl = ""; do { try { using (var wc = new WebClient()) { wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); wc.Encoding = Encoding.UTF8; strUrl = url + "&page=" + pageNo; log("打开页:" + strUrl + "<br/>"); string str = wc.DownloadString(strUrl); str = str.Replace("<script", ""); pageNo = procLine(str); //处理获取到的文本 } } catch (Exception ee) { log("获取网页时错误:" + ee.Message); } } while (pageNo > 0); using (SqlConnection conn = new SqlConnection(strConn)) { try { conn.Open(); string strSql = "delete from 线路 where 更新时间<>@更新时间"; SqlCommand cmd = new SqlCommand(strSql, conn); cmd.Parameters.Add("@更新时间", System.Data.SqlDbType.DateTime, 4).Value = dtUpdate; int sl = cmd.ExecuteNonQuery(); if (sl > 0) log("删除了未更新的线路:" + sl + "条。"); } catch (Exception ee) { log("错误:" + ee.Message); } } return retVal; }
[b]2.处理所获得的源码[/b]
int procLine(string strHtml) { int retVal = 0; HtmlDocument htmlDoc = new HtmlDocument(); // 获取html元素(htmlContext为html页面字符串) htmlDoc.LoadHtml(strHtml); // 加载html页面 HtmlNode node = htmlDoc.DocumentNode; //HtmlNodeCollection has = node.SelectNodes("//div[contains(@onclick, '/RealtimeQuery?')]"); //获取各条线路的 div HtmlNodeCollection has = node.SelectNodes("//a"); //获取各条线路的 <a href="/LineDetailQuery?lineId=1&direction=2&">1路(老福山花园站→博览城市场中路站)</a> if (has != null) foreach (HtmlNode hn in has) if (hn.Attributes.Contains("href")) { //<a href="/LineDetailQuery?lineId=1&direction=2&">1路(老福山花园站→博览城市场中路站)</a> string href = hn.Attributes["href"].Value; if (href.StartsWith("/LineDetailQuery?lineId=")) { string[] ss = href.Split(new char[] { '?', '=', '&', ';' }, StringSplitOptions.RemoveEmptyEntries); if (ss.Length > 6) { int lineId = 0; int dir = 0; int.TryParse(ss[2], out lineId); int.TryParse(ss[5], out dir); string[] lineInfo = hn.InnerText.Split(new char[] { '(', '→', ')' }, StringSplitOptions.RemoveEmptyEntries); string lineName = lineInfo[0]; string from = lineInfo[1]; string to = lineInfo[2]; int bh = lineId + dir * 100000; string str = "lineId={0}, dir={1}, lineName={2}, from={3}, to={4} <br/>"; log(string.Format(str, lineId, dir, lineName, from, to)); //将线路更新到数据库 using (SqlConnection conn = new SqlConnection(strConn)) { try { conn.Open(); string strSql = "update 线路 set 线路号=@线路号, 行向=@行向, 线路名称=@线路名称, 始发站点=@始发站点, 开往方向=@开往方向, 更新时间=@更新时间 where 编号=@编号"; SqlCommand cmd = new SqlCommand(strSql, conn); cmd.Parameters.Add("@线路号", System.Data.SqlDbType.Int, 4).Value = lineId; cmd.Parameters.Add("@行向", System.Data.SqlDbType.Int, 4).Value = dir; cmd.Parameters.Add("@线路名称", System.Data.SqlDbType.NVarChar, 50).Value = lineName; cmd.Parameters.Add("@始发站点", System.Data.SqlDbType.NVarChar, 50).Value = from; cmd.Parameters.Add("@开往方向", System.Data.SqlDbType.NVarChar, 50).Value = to; cmd.Parameters.Add("@更新时间", System.Data.SqlDbType.DateTime, 4).Value = dtUpdate; cmd.Parameters.Add("@编号", System.Data.SqlDbType.Int, 4).Value = bh; if (cmd.ExecuteNonQuery() == 0) { cmd.CommandText = "insert into 线路(编号, 线路号, 行向, 线路名称, 始发站点, 开往方向, 更新时间)values(@编号, @线路号,@行向,@线路名称,@始发站点,@开往方向,@更新时间)"; cmd.ExecuteNonQuery(); } } catch (Exception ee) { log("更新线路到数据库时错误:" + ee.Message); } } } } } //<font style="font-size:13px;">[2/26]</font> HtmlNodeCollection hasPage = node.SelectNodes("//font"); //获取各条线路的 <a href="/LineDetailQuery?lineId=1&direction=2&">1路(老福山花园站→博览城市场中路站)</a> if (hasPage != null) foreach (HtmlNode hn in hasPage) if (hn.Attributes.Contains("style")) if (hn.Attributes["style"].Value == "font-size:13px;") { string[] ss = hn.InnerText.Split(new char[] { '[', '/', ']' }, StringSplitOptions.RemoveEmptyEntries); if (ss.Length > 1) { int pageNo = 0; int pageTotal = 0; int.TryParse(ss[0], out pageNo); int.TryParse(ss[1], out pageTotal); if (pageTotal > 0) if (pageNo < pageTotal) { log("=================第" + pageNo + "页/共" + pageTotal + "页===========<br/><br/>"); retVal = pageNo + 1; } else log("==================最后一页==============="); } } return retVal; }
注:HtmlDocument、HtmlNode、HtmlNodeCollection三个类需要引用一个类库文件。该文件在博客园的后台文件管理中。
相关文章推荐
- 两个获取http页面的c#函数
- 使用C#抓取网页内容并分析获取数据
- [C#]一个简单的获取网页源代码的函数
- 两个获取http页面的c#函数(转)
- C# C/S程序请求网页地址,并获取返回数据代码
- sql实现从两个表获取字段组成表数据再插入到函数表中
- 如何用C#代码获取某网页的内容(数据),
- C# 获取网页数据、获取本机IP
- c#获取网页代码、数据、资源
- C#获取网页信息并存入数据库
- 自写获取网页HTML的C#函数
- 使用libcurl 函数库获取网页数据代码
- 写两个通用函数统一javascript获取form数据的方式
- C# 获取网页数据、获取本机IP 分类: C# 2014-12-16 14:59 308人阅读 评论(0) 收藏
- c#网页方式获取新浪微博的微博数据
- 表单提交数据的方式 怎么在服务器获取 服务器控件 html服务器控件 提交服务器之前的验证 前台网页编写c#代码
- perl post函数获取网页内容及解析数据的例子
- 两个获取http页面的c#函数
- c# 采集 获取网页数据内容 一会超时的问题
- 写两个通用函数统一javascript获取form数据的方式