HttpWebRequest图片采集

string url = "http:";
        public Form1()
        {
            InitializeComponent();
            
            this.Controls.OfType<PictureBox>().ToList().ForEach(x => x.Click += pictureBox_Click);//给窗体的所有pictureBox注册pictureBox_Click事件
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List < string> webHtml=new List<string> ();
            List<string> webTitle = new List<string>();
            //string[] webTitle;
            HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url);
            web.Proxy = null;
            HttpWebResponse weatherResponse = (HttpWebResponse)web.GetResponse();
            //从Internet资源返回数据流
            Stream webStream = weatherResponse.GetResponseStream();//
            StreamReader reader = new StreamReader(webStream, Encoding.UTF8 );//获取当前系统的编码
            string Html = reader.ReadToEnd();//读取流
            webStream.Close();
            reader.Close();//关闭流
            textBox2.Text = Html;
            string zhze = @"(?is)<span class='info_img'.*?title='(?<title>.*?)'.*?<img.*?src=../(?<img>.*?)alt";  //"<span class='info_img' ></span>;
            Regex pipei = new Regex(zhze,RegexOptions.Multiline);//(?is)<span class='info_img'.*?<img.*?(?<img>.*?)
            if (pipei.IsMatch(Html))
            {
                MatchCollection strs = pipei.Matches (Html);
                foreach (Match str in strs )
                {
                    webHtml.Add(str.Groups["img"].Value);
                    webTitle.Add(str.Groups["title"].Value);
                }
                //Match str = pipei.Match(webHtml);
                //webHtml = str.Groups["img"].Value ;
                //webTitle = str.Groups["title"].Value;
                //MessageBox.Show(webHtml);

            }
            else
            {
                MessageBox.Show("错误");
            }
            HttpWebRequest web1;
            HttpWebResponse weatherResponse1;
            Stream webStream1;
            for (int i=1;i<webHtml.Count;i++)
            {
                if (i > 6) return;
                web1 = (HttpWebRequest)HttpWebRequest.Create("http://" + webHtml[i]);
                 //web1.Proxy = null;
                 weatherResponse1 = (HttpWebResponse)web1.GetResponse();
                 //从Internet资源返回数据流
                 webStream1 = weatherResponse1.GetResponseStream();//
                 PictureBox text =(PictureBox)this.Controls["PictureBox" + i.ToString()];
                 Label lb = (Label)this.Controls["Label" + i.ToString()];
                 text.Image = Image.FromStream(webStream1);
                 lb.Text = webTitle[i];
                 webStream1.Close();
                
            }
            
        }
图片测试,留个脚印
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值