抓取网页图片

   private void button1_Click(object sender, EventArgs e) //获取网页源代码测试一下
        {
            if (textBox1.Text != "")
            {
                HttpWebRequest qingqiu = (HttpWebRequest)WebRequest.Create(textBox1.Text);//向网页发出请求
                HttpWebResponse huiying = (HttpWebResponse)qingqiu.GetResponse();//得到回应
                Stream sr = huiying.GetResponseStream();//将回应得到的内容存到sr里面,以此来转换为StreamReader 所需的参数
                StreamReader srd = new StreamReader(sr, Encoding.UTF8);//存入StreamReader 
                txtyuanma.Text = srd.ReadToEnd();//将网页源码从头读到尾并存到txtyuanma里面
                //关闭流
                sr.Close();
                srd.Close();
            }
            else
            {
                MessageBox.Show("请输入网页地址");
            }
        }

 

 

 

//////////////

 private void button2_Click(object sender, EventArgs e) //获取网页中的图片地址
        {
            if (txtyuanma.Text != "")
            {
                string yuanma = txtyuanma.Text;//定义一个变量来接收网页源码
                //创建一个区域集合来存放正则匹配后的图片地址 //RegexOptions.IgnoreCase 无视大小写
                MatchCollection mc = Regex.Matches(yuanma,@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>",RegexOptions.IgnoreCase);
                //下面开始遍历并将这所有的图片地址全部存到listview1里面
                foreach (Match mcc in mc)
                {
                    listView1.Items.Add(mcc.Value.ToString());//所有的图片地址全部存到了listview1里面了
                }
            }
            else
            {

                MessageBox.Show("请先获取网页源码");
            }
        }

 

 

 

 

/////////////////

 private void button3_Click(object sender, EventArgs e) //开始下载所有图片
        {
            if (listView1.Items[0].Text != null)
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    string imgurl = listView1.Items[i].Text;
                    MatchCollection mcol = Regex.Matches(imgurl, @"<img.*?src=""(?<src>[^""]*)""[^>]*>");//匹配图片地址
                    //开始遍历
                    foreach (Match mcoll in mcol)
                    {
                        //得到了图片的地址,下面开始下载图片
                        HttpWebRequest ww = (HttpWebRequest)WebRequest.Create(mcoll.Groups["src"].Value);//发出对图片的请求
                        HttpWebResponse wr = (HttpWebResponse)ww.GetResponse(); //反馈请求
                        Stream srr = wr.GetResponseStream();
                        string path = @"C:\Users\Administrator\Desktop\"+i.ToString()+".jpg";
                        FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write);
                        //造一个字节类型的数组来存放图片
                        byte[] buff = new byte[512];
                        int c = 0;
                        while ((c=srr.Read(buff,0,buff.Length))>0)
                        {
                            fs.Write(buff,0,c);
                        }
                        srr.Close();
                    }
                }
                MessageBox.Show("下载成功");

            }
            else
            {
                MessageBox.Show("请先获取要下载的图片地址");
            
            }
        }

posted @ 2015-06-21 15:31  XCml  阅读(998)  评论(0编辑  收藏  举报