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();
}
}
图片测试,留个脚印