C# 自动爬图工具 为了方便爬MM图片

  1        protected string fullName = string.Empty;
  2        protected int TopCount = 0;
  3
  4        private void button1_Click(object sender, EventArgs e)
  5        {
  6            Application.DoEvents();
  7
  8            this.button1.Enabled = false;
  9            this.tbCount.Enabled = false;
 10            this.tbKey.Enabled = false;
 11            
 12            try
 13            {
 14                 TopCount =  Convert.ToInt32(this.tbCount.Text);
 15            }

 16            catch
 17            {
 18                MessageBox.Show("输入数字");
 19            }

 20
 21            string HTML = string.Empty;
 22            for (int x = 0; x < TopCount; x++)
 23            {
 24
 25                Application.DoEvents();
 26
 27                string url = @"http://image.baidu.com/i?z=3&cl=2&ct=201326592&sn=&lm=-1&cm=1&sc=0&bu=&rn=21&tn=baiduimage&word={0}&pn={1}";
 28                string Key = System.Web.HttpUtility.UrlEncode(tbKey.Text, System.Text.UnicodeEncoding.GetEncoding("GB2312")).ToUpper();
 29
 30                int ctr = 21 * x;
 31
 32                url = string.Format(url, Key, ctr);
 33                Application.DoEvents();
 34
 35                WebRequest myRequest = WebRequest.Create(url);
 36                WebResponse myResponse = myRequest.GetResponse();
 37                Stream S = myResponse.GetResponseStream();
 38                StreamReader sr = new System.IO.StreamReader(S, Encoding.Default);
 39
 40                HTML += sr.ReadToEnd();
 41
 42                sr.Close();
 43            }

 44           
 45
 46
 47
 48            string Pattern = @"http:\/\/[^\'\""]*\.jpg|http:\/\/[^\'\""]*\.gif";
 49            System.Text.RegularExpressions.Regex Re = new System.Text.RegularExpressions.Regex(Pattern);
 50
 51            TopCount = Re.Matches(HTML).Count;
 52
 53
 54
 55            int count = 0;
 56            if (Re.Matches(HTML).Count >= 6)
 57            {
 58                foreach (System.Text.RegularExpressions.Match m in Re.Matches(HTML))
 59                {
 60                    Application.DoEvents();
 61
 62
 63                    this.button1.ForeColor = Color.Red;
 64                    this.button1.Text = count.ToString() + "/" + TopCount;
 65
 66                    if (m.Value.IndexOf("baidu"< 0)
 67                    {
 68                        download(m.Value);
 69
 70                        Application.DoEvents();
 71
 72                        this.pictureBox1.ImageLocation = fullName;
 73
 74
 75                        count++;
 76
 77                    }

 78
 79                }

 80            }

 81
 82        }

 83
 84
 85//
 86        private void download(string url)
 87        {
 88
 89            try
 90            {
 91                Application.DoEvents();
 92
 93                string extend = url.Substring(url.LastIndexOf("."));
 94                string name = this.tbKey.Text+Guid.NewGuid().ToString();
 95                string fileName = name + extend;
 96               
 97                WebRequest myRequest = WebRequest.Create(url);
 98                Stream stream = myRequest.GetResponse().GetResponseStream();
 99                Byte[] buffer = new byte[256];
100
101                string path= @"F:\\自动爬图\\"+this.tbKey.Text+"\\";
102
103                if (Directory.Exists(path))
104                { }
105                else
106                {
107                    Directory.CreateDirectory(path);
108                }

109
110                fullName = fileName = path + fileName;
111
112
113
114                FileStream filestream = new FileStream(fullName, FileMode.Create, FileAccess.Write);
115                int sizeCount = stream.Read(buffer, 0256);
116                try
117                {
118
119                    int sum = 0;
120                    while (sizeCount > 0)
121                    {
122                        Application.DoEvents();
123
124                        filestream.Write(buffer, 0, sizeCount);
125                        sizeCount = stream.Read(buffer, 0256);
126                        sum += sizeCount;
127                        this.Text = "已传输"+sum + "字节";
128                    }

129
130                    
131                }

132                catch (Exception ex)
133                {
134
135
136                }

137                filestream.Close();
138                stream.Close();
139
140
141            }

142            catch (Exception ex)
143            {
144
145
146            }

147
148
149
150        }



posted on 2007-09-14 01:56  小子好黑  阅读(1579)  评论(7编辑  收藏  举报

导航