基於C#的百度圖片批量下載工具,

來源:互聯網
上載者:User

基於C#的百度圖片批量下載工具,
在家沒網,無聊怎麼辦?不如來看點美女圖片吧,網路快時批量下載,有空時慢慢看,嘿嘿,本人是個好人。於是這個工具的實現,那簡直是迫在眉睫啊,來看看是怎麼實現的吧。

先片吧:

這是軟體的WinForm介面,基於C#實現。
上代碼,也就100多行。

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 < count ; 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 < imgs.Count ; 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);                }            }        }    }}

右擊查看圖片,即可查看大圖。
最終效果就是這個樣子,如何,自我感覺還是可以的。

百度網盤下載:http://pan.baidu.com/s/1kT3YzXl密碼: gafi
第一個為本程式的整個解決方案,第二個為本程式編譯後的可運行程式,Windows下可以直接跑的。


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.