当前位置:编程学习 > C#/ASP.NET >>

抓取标题失败,不会前台显示,无基础,求大神修改代码!很急!

using System;
using System.Data.OleDb;//用来连接access数据库
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data;
using System.Web;
using System.Configuration;
using System.Diagnostics;
namespace spider
{
    public partial class Form1 : Form
    {
        string MycnnStr;
       
        OleDbConnection Mycnn = new OleDbConnection();
        OleDbCommand cmd = new OleDbCommand();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            
            //TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
            string url = textBox1.Text.ToString();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
            TextReader tr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
            textBox2.Text = tr.ReadToEnd();
            response.Close();
            WebExpress2(textBox2.Text.ToString());
           
            WebExpress1(textBox4.Text.ToString());
            sw.Stop();
            MessageBox.Show("耗时" + sw.ElapsedMilliseconds.ToString() + "毫秒");
            //TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            //TimeSpan ts = ts2.Subtract(ts1).Duration();
            //string spanTotalSeconds = ts.TotalSeconds.ToString();
            //string spanTime = ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒";
            //MessageBox.Show(spanTime);
        }
        private void WebExpress3(string s)
        {
            string url = textBox1.Text.ToString();
          DateTime start = DateTime.Now; 
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream ReceiveStream = res.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            StreamReader sr = new StreamReader(ReceiveStream, encode);
 
            string strResult = "";
            Char[] read = new Char[256];
            int count = sr.Read(read, 0, 256);
            while (count > 0)
            {
                String str = new String(read, 0, count);
                strResult += str;
                count = sr.Read(read, 0, 256);
                if (strResult.IndexOf("body") != -1)
                {
                    break;
                }
            }
}

        private void WebExpress1(string s)//抓取网页标题
        {
            string tag1 = "<  title=" ;
            string tag2 = " >";
            int pos1 = s.IndexOf(tag1);
            int pos2, pos3;
            while (pos1 > 0)
            {
                pos2 = s.IndexOf(tag2, pos1 + 1);
                if (pos2 < 0)
                    return;
                pos3 = s.IndexOf(" ", pos1 + 10);
                if (pos3 > 0 && pos3 < pos2)
                    pos2 = pos3;
                string data = s.Substring(pos1 + tag1.Length, pos2 - pos1 - tag1.Length);
                data = data.Replace(">", "");
                data = data.Replace("\"", "");
                int pos4 = data.IndexOf("title");
                if (pos4 < 0)
                textBox4.Text += data + "\n";
                s = s.Substring(pos2 + 1);
                pos1 = s.IndexOf(tag1);
            }
        }

        private void WebExpress2(string s)//抓取网页链接
        {
            string tag1 = "<a href=";
            string tag2 = "</a>";
            int pos1 = s.IndexOf(tag1);
            int pos2, pos3;
            while (pos1 > 0)
            {
                pos2 = s.IndexOf(tag2, pos1 + 1);
                if (pos2 < 0)
                    return;
                pos3 = s.IndexOf(" ", pos1 + 4);
                if (pos3 > 0 && pos3 < pos2)
                    pos2 = pos3;
                string url = s.Substring(pos1 + tag1.Length, pos2 - pos1 - tag1.Length);
                url = url.Replace("\"", "");
                int pos4 = url.IndexOf("http");
                if (pos4 < 0)
                    url = textBox1.Text.ToString() + url;
                textBox3.Text += url + "\n";
                s = s.Substring(pos2 + 1);
                pos1 = s.IndexOf(tag1);
            }
        }

        private void button2_Click(object sender, EventArgs e)//将数据存入数据库
        {
            MycnnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Administrator\Desktop\学业\信息组织与检索\table.mdb";
            Mycnn.ConnectionString = MycnnStr;
            Mycnn.Open();
            cmd.Connection = Mycnn;
            string strSQL = textBox3.Text;
            string[] strArr = strSQL.Split('\n');
            for (int i = 0; i <= (strArr.Length - 2); i++)
            {
                string str = "insert into webaddress(webad)values('" + strArr[i] + "')";
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
            }

            Mycnn.Close();
        }


        private void button3_Click(object sender, EventArgs e)
        {
            MycnnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Administrator\Desktop\学业\信息组织与检索\table.mdb";
            Mycnn.ConnectionString = MycnnStr;
            Mycnn.Open();
            cmd.Connection = Mycnn;
            string strSQL1 = textBox4.Text;
            string[] strArr1 = strSQL1.Split('\n');
            for (int i = 0; i <= (strArr1.Length - 2); i++)
            {
                string str = "insert into webaddress(title)values('" + strArr1[i] + "')";
                cmd.CommandText = str;
                cmd.ExecuteNonQuery();
            }
            Mycnn.Close();
        }
       

        private void urlTitle(string s)
        {


        }
    }
} c#   microsoft visual access --------------------编程问答-------------------- 难道我们是同学?来找卢哲东吧....
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,