抓去网站上的信息插入到数据库,测试OK!

private void button1_Click(object sender, EventArgs e)
        {
            dt.Rows.Clear();
            //要抓取的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");

            // DataRow row = null;
            foreach (HtmlElement tr in htmlTR)
            {
                string strID = tr.GetElementsByTagName("TD")[0].InnerText;
                string[] info = tr.GetElementsByTagName("TD")[1].InnerText.Split('(');
                string strName = "";
                string strSinger = "";
                if (info.Length != 2)
                {
                    strName = info[0];
                    strSinger = "未知";
                }
                else
                {
                    strName = info[0];
                    strSinger = info[1].Replace(")", "");
                }
                strID = strID.Replace(".", "");
                DataRow row = dt.NewRow();
                row["ID"] = strID;
                row["voide"] = strName;
                row["name"] = strSinger;
                dt.Rows.Add(row);


                string strID1 = tr.GetElementsByTagName("TD")[2].InnerText;
                string[] info1 = tr.GetElementsByTagName("TD")[3].InnerText.Split('(');
                string strName1 = "";
                string strSinger1 = "";
                if (info1.Length != 2)
                {
                    strName1 = info1[0];
                    strSinger1 = "未知";
                }
                else
                {
                    strName1 = info1[0];
                    strSinger1 = info1[1].Replace(")", "");
                }
                strID1 = strID1.Replace(".", "");
                DataRow row1 = dt.NewRow();
                row1["ID"] = strID1;
                row1["voide"] = strName1;
                row1["name"] = strSinger1;
                dt.Rows.Add(row1);

 

                //string strID2 = tr.GetElementsByTagName("TD")[3].InnerText;

                //string[] info2 = tr.GetElementsByTagName("TD")[4].InnerText.Split('(');
                //string strName2 = "";
                //string strSinger2 = "";
                //if (info1.Length != 2)
                //{
                //    strName2 = info1[0];
                //    strSinger2 = "未知";
                //}
                //else
                //{
                //    strName2 = info1[0];
                //    strSinger2 = info1[1].Replace(")", "");
                //}
                //strID1 = strID2.Replace(".", "");
                //DataRow row2 = dt.NewRow();
                //row2["ID"] = strID2;
                //row2["voide"] = strName2;
                //row2["name"] = strSinger2;
                //dt.Rows.Add(row2);

            }
            插入数据库
            InsertData(dt);

            dataGridView1.DataSource = dt.DefaultView;

 

        }
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();

                //关闭流

                streamReader.Close();

                //关闭网络响应流

                response.Close();
            }
            catch
            {
                MessageBox.Show("出错");
            }
            return strResult;
        }
        static DataTable dt = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {
            DataColumn ID = new DataColumn();
            DataColumn voideName = new DataColumn();
            DataColumn name = new DataColumn();
            ID.ColumnName = "ID";
            voideName.ColumnName = "voide";
            name.ColumnName = "name";
            dt.Columns.Add(ID);
            dt.Columns.Add(voideName);
            dt.Columns.Add(name);
        }

 

 

 

T1WI0zXjhzXXX_QYg2_043230.jpg_sum.jpg
10年夏款 Youngor雅戈尔 男式翻领短袖T恤 男式纯棉T恤3901米黄
118.0元 fgetccode_btn.gif

 

 

T1.hRBXfxCXXay84Yb_124327.jpg_sum.jpg
OOU!官方旗舰店!2GB 酷狗狗黑骨头 U盘 优盘 U-A01-2G
159.0元 fgetccode_btn.gif

转载于:https://www.cnblogs.com/lidaohang/archive/2010/06/13/1757808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值