C#实现通过程序自动抓取远程Web网页信息的代码

2016-02-19 11:07 40 1 收藏

今天图老师小编给大家精心推荐个C#实现通过程序自动抓取远程Web网页信息的代码教程,一起来看看过程究竟如何进行吧!喜欢还请点个赞哦~

【 tulaoshi.com - 编程语言 】

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

来源:https://www.tulaoshi.com/n/20160219/1596571.html

延伸阅读
SQLHelper.cs 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace HelloWinForm.DBUtility { class SQLHelper { #region 通用方法 // 数据连接池 private SqlConnection con; /...
如果叫你实现远程启动别人的计算机,你首先想到的可能是先做一个在远程计算机上面运行客户端程序,然后在本地计算机上面再做一个服务器端程序,通过这二个程序直接的通讯实现重启远程计算机。这当然是一个方法。但这未免有点麻烦。如果现在只告诉你远程计算机的管理者的登陆帐号,而并不允许你在远程的计算机上面运行一个所谓的客户端程序,让你...
WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间采用超文本传送协议(HTTP)进行通信,HTTP协议的作用原理包括四个步骤:连接,请求,应答。根据上述HTTP协议的作用原理,本文实现了GET请求的Web服务器程序的方法,通过创建TcpListener类对象,监听端口8080; 等待、接受客户机连接到端口8080...
摘要  想必大家对小榕时光等扫描器都非常熟悉了,有没有自己写一个的冲动。最近微软推实施了.NET战略方案,C#是主推语言,你们是否有兴趣用C#来实现对局域网IP地址的扫描,尝试一下自己写的快乐,那么请跟我来。  正文  1.先介绍一下使用的类:  DNS类:在.net中的System.net命名空间下,主要的功能是从 Internet&...
通过编程方式实现短信息的发送对很多人来说是一件比较烦杂的事情,目前一般的解决方法是通过计算机和手机的连线,通过可对手机编程的语言编写相关的手机短信息程序来实现,而这种方法对于一般人来说是很难达到的,因为此种方法不仅要有很多必备的硬件设备,也还需懂得手机编程的相关知识。本文就来探讨一下另外一种通过Visual C#发送短信息...

经验教程

838

收藏

44
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部