C# WEB网页内容采集

抓取Web网页数据分析
通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序。比方说我们有一个系统,要提取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;

        }

 

        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 =

                "<a href='http://list.mp3.baidu.com/topso/" +

                "mp3topsong.html?id=1#top2' target='_blank'>" +

                "http://list.mp3.baidu.com/...</a>";   

  

            //得到指定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;   

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
预览图片见:http://www.cnblogs.com/xxpyeippx/archive/2008/03/31/1131211.html运行环境windows nt/xp/2003 or above.net Framework 1.1SqlServer 2000 开发环境 VS 2003目的学习了网络编程,总要做点什么东西才好。于是想到要做一个网页内容采集器。作者主页: http://www.fltek.com.cn使用方式测试数据采用自cnBlog。见下图用户首先填写“起始网页”,即从哪一页开始采集。然后填写数据库连接字符串,这里是定义了采集到的数据插入到哪个数据库,后面选择表名,不必说了。网页编码,不出意外的话,中国大陆都可以采用UTF-8爬取文件名的正则:呵呵 这个工具明显是给编程人员用的。正则都要直接填写啦。比如说cnblogs的都是数字的,所以写了\d建表帮助:用户指定要建立几个varchar型的,几个text型的,主要是放短数据和长数据啊。如果你的表里本来就有列,那就免啦。程序里面没有做验证哦。网页设置里面:采集内容前后标记:比如说都有 xxx,如果我要采集xxx就写“到”,意思,当然就是到之间的内容啦。后面的几个文本框是显示内容的。点击“获取URL”可以查看它捕获的Url对不对的。点击“采集”,可以把采集内容放到数据库,然后就用 Insert xx () (select xx) 可以直接插入目标数据了。程序代码量常小(也常简陋),需要的改动一下啦。不足 应用到了正则表达式、网络编程由于是最简单的东西,所以没有用多线程,没有用其他的优化方法,不支持分页。测试了一下,获取38条数据,用了700M内存啊。。。。如果有用的人 ,可以改一下使用啦。方便程序员用,免写很多代码。Surance Yin@ Surance Center 转载请注明出处

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值