登录  /  注册

基于C#的百度图片批量下载工具

大家讲道理
发布: 2016-11-10 09:51:59
原创
2191人浏览过

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.IO;  
using System.Linq;  
using System.Net;  
using System.Text;  
using System.Threading;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
using Newtonsoft.Json;  
using Newtonsoft.Json.Linq;  
   
namespace 图片下载器 {  
    public partial class Form1 : Form {  
        private string dir;  
        public Form1() {  
            Control.CheckForIllegalCrossThreadCalls = false;//这种方法不推荐使用,即不检查跨线程操作,应该使用委托的  
            InitializeComponent();  
        }  
   
        private void butSelect_Click(object sender , EventArgs e) {  
            FolderBrowserDialog dlg = new FolderBrowserDialog();  
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {  
                textDir.Text = dlg.SelectedPath;  
   
            }  
        }  
        public static int pagecount = 1;  
        private void Showpages() {  
            this.textShow.AppendText("目前正在下载第" + pagecount + "页\n");  
        }  
        private void butStart_Click(object sender , EventArgs e) {  
            string key = textKeyWords.Text;  
            if (string.IsNullOrEmpty(key)) {//检测关键字  
                MessageBox.Show("请输入关键词!");  
                return;  
            }  
            if (string.IsNullOrEmpty(textDir.Text)) {//检测路径  
                MessageBox.Show("请选择路径!");  
                return;  
            }  
            dir = textDir.Text;  
            if (!dir.EndsWith("\\")) {  
                dir = dir + "\\";  
            }  
            Thread thread = new Thread(() => {//启动一个新线程  
                process(key);  
            });  
            thread.Start();//线程启动  
        }  
   
        private void process(string key) {  
            int count = (int)numericUpDown.Value;//请求的页面数量  
            for (int i = 0 ; i                 pagecount = i + 1;  
                Showpages();  
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word=" + Uri.EscapeUriString(key) + "&cg=girl&pn=" + (i + 1) * 60 + "&rn=60&itg=0&z=0&fr=&width=&height=&lm=-1&ic=0&s=0&st=-1&gsm=360600003c");  
                using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) {  
                    if (res.StatusCode == HttpStatusCode.OK) {  
                        using (Stream stream = res.GetResponseStream()) {  
                            try {  
                                download(stream);  
                            } catch (Exception e) {  
                                textShow.BeginInvoke(new Action(() => {  
                                    textShow.AppendText(e.Message + Environment.NewLine);  
                                }));  
                            }  
                        }  
                    } else {  
                        MessageBox.Show("获取第" + i + "页失败!" + res.StatusCode);  
                    }  
                }  
            }  
        }  
   
        private void download(Stream stream) {  
            using (StreamReader reader = new StreamReader(stream)) {  
                string json = reader.ReadToEnd();  
                JObject objRoot = (JObject)JsonConvert.DeserializeObject(json);  
                JArray imgs = (JArray)objRoot["imgs"];  
                for (int j = 0 ; j                     JObject img = (JObject)imgs[j];  
                    string objUrl = (string)img["objURL"];//http://hibiadu....../1.jpg  
                    // textShow.AppendText(objUrl + Environment.NewLine);  
                    //保存的路径是:destDir;  
                    try {  
                        DownloadImage(objUrl);//避免一个方法中的代码过于复杂  
                    } catch (Exception ex) {  
                        //子线程的代码中操作界面控件要使用BeginInvoke  
                        textShow.BeginInvoke(new Action(() => {  
                            textShow.AppendText(ex.Message + Environment.NewLine);  
                        }));  
                    }  
                }  
            }  
        }  
        private void DownloadImage(string objUrl) {  
            //得到保存的路径  
            string path = Path.Combine(dir , Path.GetFileName(objUrl));  
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(objUrl);  
            req.Referer = "http://image.baidu.com/";//欺骗网站服务器这是从百度图片发出的  
            using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) {  
                if (res.StatusCode == HttpStatusCode.OK) {  
                    using (Stream stream = res.GetResponseStream())  
                    using (Stream filestream = new FileStream(path , FileMode.Create)) {  
                        stream.CopyTo(filestream);  
                    }  
                } else {  
                    throw new Exception("下载失败" + res.StatusCode);  
                }  
            }  
        }  
    }  
}

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号