/// <summary> /// 获取图片url /// </summary> /// <param name="html"></param> private void MatchImageUrl(string html, string host, string webUrl) { string matchStr = "<img[\\s^\\s]+src=\"(?<src>\\S+)\"";//分组命名 string imgUrl = string.Empty; Regex regex = new Regex(matchStr, RegexOptions.IgnoreCase | RegexOptions.Compiled); MatchCollection collection = regex.Matches(html); foreach (Match item in collection) { imgUrl = item.Groups["src"].Value; //无效链接 if (string.IsNullOrEmpty(imgUrl)) { continue; } if (!imgUrl.Contains("http")) { imgUrl = "http://" + host + imgUrl; } //是否存在 if (ImageUrlBLL.IsExistImageUrl(imgUrl)) { continue; } ImageUrlEntity imageUrlEnt = new ImageUrlEntity(); imageUrlEnt.AddDate = DateTime.Now; imageUrlEnt.Host = host; imageUrlEnt.ImageUrl = imgUrl; imageUrlEnt.WebUrl = webUrl; ImageUrlBLL.Insert(imageUrlEnt); } }
/// <summary> /// 下载图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void loadImage_Click(object sender, EventArgs e) { try { string host = this.txtLoad.Text; if (string.IsNullOrEmpty(host)) { MessageBox.Show("请输入主机"); return; } CookieContainer cookie = new CookieContainer(); if (this.ckbLogin.Checked) { string msg = ""; cookie = this.GetCookie(ref msg); if (msg != "") { MessageBox.Show(msg); return; } } this.loadImage.Text = "执行中..."; List <ImageUrlEntity> imageList = ImageUrlBLL.GetList(host); foreach (var item in imageList) { RequestEntity requestEnt = new RequestEntity(); requestEnt.postUrl = item.ImageUrl; requestEnt.method = "get"; requestEnt.host = item.Host; requestEnt.referer = item.WebUrl; requestEnt.cookie = cookie; RequestHelper.LoadPic(requestEnt); ImageUrlBLL.UpdateUsered(item.Id); } this.loadImage.Text = "下载图片"; } catch (Exception ex) { LogHelper.LogTrace("报错:" + ex.Message); Application.Exit(); } }