通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序。比方说我们有一个系统,要提取BaiDu网站上歌曲搜索排名。分析系统在根据得到的数据进行数据分析。为业务提供参考数据。
  为了完成以上的需求,我们就需要模拟浏览器浏览网页,得到页面的数据在进行分析,最后把分析的结构,即整理好的数据写入数据库。那么我们的思路就是:
  1、发送HttpRequest请求。
  2、接收HttpResponse返回的结果。得到特定页面的html源文件。
  3、取出包含数据的那一部分源码。
  4、根据html源码生成HtmlDocument,循环取出数据。
  5、写入数据库。

代码如下:

//根据Url地址得到网页的html源码
        private string GetWebContent(string Url)
        {
            string strResult="";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

//声明一个HttpWebRequest请求
                request.Timeout = 30000;
                //设置连接超时时间
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.GetEncoding("GB2312");
                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                strResult = streamReader.ReadToEnd();
            }
            catch
            {
                MessageBox.Show("出错");
            }
            return strResult;
        }
为了使用HttpWebRequest和HttpWebResponse,需填名字空间引用
  using System.Net;

以下是程序具体实现过程:
 private void button1_Click(object sender, EventArgs e)
        {
            //要抓取的URL地址
            string Url = "http://list.mp3.baidu.com/topso/mp3topsong.html?id=1#top2";

//得到指定Url的源码
   string strWebContent = GetWebContent(Url);

richTextBox1.Text = strWebContent;
   //取出和数据有关的那段源码
            int iBodyStart = strWebContent.IndexOf("<body", 0);
            int iStart = strWebContent.IndexOf("歌曲TOP500", iBodyStart);
            int iTableStart = strWebContent.IndexOf("<table", iStart);
            int iTableEnd = strWebContent.IndexOf("</table>", iTableStart);
            string strWeb = strWebContent.Substring(iTableStart, iTableEnd - iTableStart + 8);

//生成HtmlDocument
   WebBrowser webb = new WebBrowser();
            webb.Navigate("about:blank");
            HtmlDocument htmldoc = webb.Document.OpenNew(true);
            htmldoc.Write(strWeb);
            HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("TR");
            foreach (HtmlElement tr in htmlTR)
            {
                string strID = tr.GetElementsByTagName("TD")[0].InnerText;
                string strName = SplitName(tr.GetElementsByTagName("TD")[1].InnerText, "MusicName");
                string strSinger = SplitName(tr.GetElementsByTagName("TD")[1].InnerText, "Singer");
                strID = strID.Replace(".", "");
                //插入DataTable
                AddLine(strID, strName, strSinger,"0");

string strID1 = tr.GetElementsByTagName("TD")[2].InnerText;
                string strName1 = SplitName(tr.GetElementsByTagName("TD")[3].InnerText, "MusicName");
                string strSinger1 = SplitName(tr.GetElementsByTagName("TD")[3].InnerText, "Singer");
                //插入DataTable
                strID1 = strID1.Replace(".", "");
                AddLine(strID1, strName1, strSinger1,"0");

string strID2 = tr.GetElementsByTagName("TD")[4].InnerText;
                string strName2 = SplitName(tr.GetElementsByTagName("TD")[5].InnerText, "MusicName");
                string strSinger2 = SplitName(tr.GetElementsByTagName("TD")[5].InnerText, "Singer");
                //插入DataTable
                strID2 = strID2.Replace(".", "");
                AddLine(strID2, strName2, strSinger2,"0");

}
            //插入数据库
            InsertData(dt);
   
            dataGridView1.DataSource = dt.DefaultView;
}

最新文章

  1. 使用vue1.0+es6+vue-cli+webpack+iview-ui+jQuery 撸一套高质量的后台管理系统
  2. Sublime Text使用配置介绍
  3. HTML5移动端图片左右切换动画
  4. 【JavaScript】js数组操作,由push到那么多
  5. iOS UITableViewCell滑动删除
  6. jquery基本选择器id
  7. unity3d 自动保存
  8. ADB调试桥安装(方式一)
  9. SpringMVC 中的Interceptor 拦截器
  10. 基本STRUTS标签-学习笔记-Logic标签
  11. Entity Framework Code First 多数据库 控制台迁移代码
  12. oracle与sql server时间差的取法
  13. js各种继承方式汇总
  14. [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素
  15. 关于openssl的交叉编译
  16. 深入浅出:5G和HTTP
  17. 十图详解tensorflow数据读取机制(附代码)转知乎
  18. sharepoint2013 Restore-SPSite 报错,采用数据库还原
  19. os模块(二十)
  20. 手动更新nexus的索引

热门文章

  1. logistic回归 c++ 实现
  2. C++中文件的操作
  3. 新认识:SDF数据库
  4. D15
  5. c# 使用Codosys.dll(CDO)发送邮件
  6. C#程序调用cmd.exe执行命令
  7. MSSQL数据库迁移到Oracle
  8. revel框架教程之缓存和Job
  9. HighCharts 图表高度动态调整
  10. 不再害羞,过程比结果更重要;分享一套 CodeSmit 代码生成模板。