/// <summary> /// 抽取Cosplay图片 /// </summary> /// <param name="respHtml">网页源代码</param> /// <returns></returns> private ImgInfo ExctractDaily(string respHtml) { ImgInfo img = new ImgInfo(); img.Kind = ImgKind.Daily; img.Urls = new List <string>(); //获取图片归属作品名称 Match ACGWork = Regex.Match(respHtml, @"<meta name=""keywords"" content=""(?<info>.+?),+.*"" />"); img.ACGWork = ACGWork.Groups["info"].Value; //获取Author信息 Regex cnReg = new Regex(@"<img.+alt=""(?<info>.+?)""/>"); Match cn = cnReg.Match(respHtml); img.Author = cn.Groups["info"].Value; //获取标题 Match titleMat = Regex.Match(respHtml, "<h1.*>\n(?<info>.+?) </h1>"); try { if (titleMat.Value == null) { img.Title = ""; } else { img.Title = titleMat.Groups["info"].Value; } } catch (Exception) { string tilte = titleMat.Value; int index1 = tilte.IndexOf('\n'); int index2 = tilte.IndexOf('<', index1); img.Title = tilte.Substring(index1 + 1, index2 - index1); } //获取正文 Match descriptionMatch = Regex.Match(respHtml, @"<div id=""larticleArticle"" class=""l-clearfix larticleArticle"">(?<info>.+?)</div>"); string description = descriptionMatch.Groups["info"].Value; //获取<a>标签内链接和内容 Regex desreg = new Regex(@"(?is)<a(?:(?!href=).)*href=(['""]?)(?<url>[^""\s>]*)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>"); MatchCollection des = desreg.Matches(description); foreach (Match x in des) { description = description.Replace(x.Value, x.Groups["text"].Value); } description = description.Replace("<br>", System.Environment.NewLine); img.Description = description; //获取链接 Regex linkreg = new Regex(@"<img class='detail_std detail_clickable' src='(?<info>.+?)/w650' />"); MatchCollection urls = linkreg.Matches(respHtml); foreach (Match x in urls) { string str; str = x.Groups["info"].Value; if (!string.IsNullOrEmpty(str)) { img.Urls.Add(str); } } return(img); }
private void GetImages_Click(object sender, EventArgs e) { GetImgInfo getImgInfo = new GetImgInfo(); string url = txt_Url.Text; img = getImgInfo.GetImgUrls(url); //判断抓取中是否遇到错误! if (!string.IsNullOrEmpty(img.Error)) { MessageBox.Show(img.Error); return; } //获取标题并居中 Title.Text = img.ACGWork + " " + Regex.Replace(img.Title + " - " + img.Author, @"&", @"&&") + "(" + img.Urls.Count + "P)"; Title.Location = new Point((this.Width - 20 - Title.Width) / 2, Title.Location.Y); //重置打开文件夹按钮状态 btn_OpenFloder.Enabled = false; //清空已有图片 flp_ImgPriview.Controls.Clear(); //加载描述 TextBox description = new TextBox(); description.ShortcutsEnabled = true; description.Multiline = true; description.Margin = new System.Windows.Forms.Padding(3); description.Name = "description"; description.Size = new Size(300, 300); //description.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; description.Text = img.Description; flp_ImgPriview.Controls.Add(description); //加载预览图片 for (int i = 0; i < img.Urls.Count; i++) { PictureBox pic = new PictureBox(); pic.Margin = new Padding(3); pic.Name = "pic" + i.ToString(); pic.Size = new System.Drawing.Size(300, 300); //图片边框重绘 pic.Paint += new PaintEventHandler(pic_Paint); ////设置填充方式 flowlayout表格中不能设置为Fill //pic.Dock = System.Windows.Forms.DockStyle.Fill; //设置图片显示方式及链接位置 pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; pic.ImageLocation = img.Urls[i] + "/w650"; //更换等待动画 pic.InitialImage = global::MyTools.Properties.Resources.loading; //鼠标单击事件 pic.MouseDown += new MouseEventHandler(pic_MouseDown); //将图片添加到flowlayoutpanel flp_ImgPriview.Controls.Add(pic); ////将图片添加到tablelayoutpanel //ImagesPriview.Controls.Add(pic, i % 3, i / 3); ////鼠标点击事件 //pic.MouseDown += new MouseEventHandler(pic_MouseDown); } //检查目录和文件名,将不能使用字符替换为“_” //正则表达式"[\\u005C/:\\u002A\\u003F\"<>\'\\u007C’‘“”:?]"还包含中文的字符(实际上中文字符是可以使用的) string fileNameCheck = "[\\u005C/:\\u002A\\u003F\"<>\'\\u007C]"; string titleCheck = Regex.Replace(img.ACGWork, fileNameCheck, "_"); string pathCheck = ""; string path = ""; switch (img.Kind) { case ImgKind.Cosplay: pathCheck = Regex.Replace(img.Title + " - " + img.Author, fileNameCheck, "_"); path = @"E:\图片\Cosplay\" + titleCheck + @"\" + pathCheck; break; case ImgKind.Daily: pathCheck = Regex.Replace(img.Author, fileNameCheck, "_"); path = @"E:\图片\Cosplay\日常\" + pathCheck; break; case ImgKind.Illustraion: pathCheck = Regex.Replace(img.Author, fileNameCheck, "_"); path = @"E:\图片\画师\" + pathCheck; break; } txt_FloderPath.Text = path; flp_ImgPriview.Select(); }