写了个下载百度贴吧某个贴子,所有图片的工具
界面
源码
namespace 下载某贴图片 { public partial class 百度贴吧贴子图片批量下载 : Form { public 百度贴吧贴子图片批量下载() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); this.textBox2.Text = folderBrowserDialog1.SelectedPath; } private void getimg() { //加关注http://tieba.baidu.com/i/data/panel?un=%D7%CF%BB%EA%C7%E0%D0%C4&t=0.5068710697690375&fid=6031967# //得到首页路径 // GetWebContent string url = textBox1.Text.Trim(); int wenhaoindex = url.IndexOf("?"); if (wenhaoindex > -1) { url = url.Substring(0, wenhaoindex); } if (checkBox1.Checked == true) { url = url + "?see_lz=1"; } //得到第一页。 string firstpage = GetWebContent(url); Regex reg = new Regex("(?<=共有<span class=\"red\">)\\w+(?=</span>页)"); string pagecount = reg.Match(firstpage).Value; for (int i = 1; i <= Convert.ToInt16(pagecount); i++) { string thispage = ""; if (i == 1) { thispage = firstpage; } else { if (checkBox1.Checked == true) { thispage = GetWebContent(url + "&pn=" + i + ""); } else { thispage = GetWebContent(url + "?pn=" + i + ""); } } getwebimg(thispage); } } private void button2_Click(object sender, EventArgs e) { Thread oThread = new Thread(new ThreadStart(getimg)); oThread.Start(); } //根据Url地址得到网页的html源码 private string GetWebContent(string Url) { string strResult = ""; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); //声明一个HttpWebRequest请求 // request.Timeout = 10000; //设置连接超时时间 request.Headers.Set("Pragma", "no-cache"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream streamReceive = response.GetResponseStream(); Encoding encoding = Encoding.GetEncoding("GB2312"); StreamReader streamReader = new StreamReader(streamReceive, encoding); strResult = streamReader.ReadToEnd(); } catch { MessageBox.Show("出错"); } return strResult; } //得到页面的所有图片路径 private void getwebimg(string pagecount) { ArrayList allsrc = new ArrayList(); // (?<="d_post_content j_d_post_content">).+(?=</div>) Regex reg = new Regex("(?<=\"d_post_content j_d_post_content\">).+(?=</div>)"); MatchCollection imgstag = reg.Matches(pagecount); for (int i = 0; i < imgstag.Count; i++) { //匹配IMG // string imgta = imgstag[i].Value; //<img .+> Regex regimg = new Regex("<img class=\"BDE_Image\" .+>"); MatchCollection imgs = regimg.Matches(imgstag[i].Value); for (int j = 0; j < imgs.Count; j++) { //(?<=src=\")\S+(?=\") Regex regsrc = new Regex("(?<=src=\")\\S+(?=\")"); MatchCollection srcs = regsrc.Matches(imgs[j].Value); foreach (Match src in srcs) { //BDE_Smiley2 //if (src.Value.IndexOf("BDE_Image") >= -1) //{ allsrc.Add(src.Value); //} } } } for (int k = 0; k < allsrc.Count; k++) { try { Thread thread = new Thread(new ParameterizedThreadStart(SaveRemotePhoto)); thread.Start(allsrc[k].ToString()); // SaveRemotePhoto(allsrc[k].ToString()); } catch { } } } /// <summary> /// 抓取远程图片 /// </summary> /// <param name="fileName">如果是要缩略图,记得图片名以_thumbs.jpg结尾</param> /// <param name="RemoteFileUrl"></param> /// <param name="width">最后的宽,0表示原来尺寸</param> /// <param name="height">最后的高,0表示原来尺寸</param> /// <returns></returns> public void SaveRemotePhoto(object RemoteFile) { string RemoteFileUrl = RemoteFile as string; try { //为防止图片未能本地化后,内网比较纠结,干脆抓取不到时直接报错 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RemoteFileUrl); request.Timeout = 20000; request.KeepAlive = false; Stream stream = request.GetResponse().GetResponseStream(); System.Drawing.Image getImage = System.Drawing.Image.FromStream(stream); string fileName = GetFileName(true, RemoteFileUrl); string dirpath = textBox2.Text; fileName = dirpath + "\\" + fileName; try { getImage.Save(fileName); } catch { System.Drawing.Bitmap t = new System.Drawing.Bitmap(getImage); t.Save(fileName, ImgFormat(fileName)); t.Dispose(); } getImage.Dispose(); // true; } catch (Exception) { // return false; } finally { } //if (width > 0 && height > 0)//数据流读图片 //{ // WebRequest request = WebRequest.Create(RemoteFileUrl); // request.Timeout = 20000; // Stream stream = request.GetResponse().GetResponseStream(); // System.Drawing.Image getImage = System.Drawing.Image.FromStream(stream); // Common.New.ImageHelp.Image2Thumbs(getImage, fileName, width, height, "Fill"); // getImage.Dispose(); //} //else//直接下载 //{ // WebClient client = new WebClient(); // client.DownloadFile(RemoteFileUrl, fileName); //} //return true; } /// <summary> /// 获得图片的类型 /// </summary> /// <param name="_Photo"></param> /// <returns></returns> public static System.Drawing.Imaging.ImageFormat ImgFormat(string _Photo) { //获得图片的后缀,不带点,小写 string imgExt = _Photo.Substring(_Photo.LastIndexOf(".") + 1, _Photo.Length - _Photo.LastIndexOf(".") - 1).ToLower(); System.Drawing.Imaging.ImageFormat _ImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; switch (imgExt) { case "png": _ImgFormat = System.Drawing.Imaging.ImageFormat.Png; break; case "gif": _ImgFormat = System.Drawing.Imaging.ImageFormat.Gif; break; case "bmp": _ImgFormat = System.Drawing.Imaging.ImageFormat.Bmp; break; default: _ImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break; } return _ImgFormat; } public static string GetFileName(bool isUrl, string filePath) { if (isUrl) return filePath.Substring(filePath.LastIndexOf("/") + 1, filePath.Length - filePath.LastIndexOf("/") - 1); else return filePath.Substring(filePath.LastIndexOf("\\") + 1, filePath.Length - filePath.LastIndexOf("\\") - 1); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("explorer.exe", "http://www.baidu.com/p/紫魂青心"); } private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("explorer.exe", "http://www.cnblogs.com/zihunqingxin/"); } } }