zoukankan      html  css  js  c++  java
  • 抓取网页图片

       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[^<>]*?src[s	
    ]*=[s	
    ]*[""']?[s	
    ]*(?<imgUrl>[^s	
    ""'<>]*)[^<>]*?/?[s	
    ]*>",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:UsersAdministratorDesktop"+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("请先获取要下载的图片地址");
                
                }
            }

  • 相关阅读:
    Jdbc增删改查的相关操作(Oracle 数据库环境)
    java
    今日随笔
    爬虫之链家网
    爬虫之搜狗
    【题解】「UVA1149」装箱 Bin Packing
    【题解】「SP34013」SEUG
    【题解】「SP867」 CUBES
    【题解】NOI 系列题解总集
    APIO2019简要题解
  • 原文地址:https://www.cnblogs.com/lk-kk/p/4591996.html
Copyright © 2011-2022 走看看