Baidu image batch download Tool Based on C,
What should I do if I have no network connection at home? Let's take a look at beautiful pictures. You can download them in batches when the network is fast and watch them when you are free. Hey, I am a good guy. So the implementation of this tool is imminent. Let's see how it is implemented.
First slice:
This is the WinForm interface of the software, which is implemented based on C.
The code is more than 100 lines.
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 image download tool {public partial class Form1: Form {private string dir; public Form1 (){ Control. checkforillegalcrossthreadcils = false; // This method is not recommended, that is, cross-thread operations are not checked. You should use the delegate 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. appendTex T ("currently downloading" + pagecount + "Page \ n");} private void butStart_Click (object sender, EventArgs e) {string key = textKeyWords. text; if (string. isNullOrEmpty (key) {// check the keyword MessageBox. show ("Enter keywords! "); Return;} if (string. IsNullOrEmpty (textDir. Text) {// check path MessageBox. Show (" select the path! "); Return;} dir = textDir. Text; if (! Dir. endsWith ("\") {dir = dir + "\";} Thread thread = new Thread (() =={// start a new thread process (key) ;}); thread. start (); // thread Start} private void process (string key) {int count = (int) numericUpDown. value; // number of requested pages 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. beginInv Ke (new Action () => {textShow. appendText (e. message + Environment. newLine) ;}) ;}}} else {MessageBox. show ("retrieve page" + I + "failed! "+ 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 {textShow. appendText (ex. message + Environment. newLine) ;}}}} private void DownloadImage (string objUrl) {// get the saved path string Path = path. combine (dir, Path. getFileName (objUrl); HttpWebRequest req = (HttpWebRequest) WebRequest. create (objUrl); req. referer = "http://image.baidu.com/"; // spoof the website server which is sent from Baidu Image 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 ("Download failed" + res. statusCode );}}}}}
Right-click to view the image.
The final effect is like this. How can we feel ourselves.
Download Baidu online storage: http://pan.baidu.com/s/1kt3yzxlpassword: gafi
The first is the entire solution of this program, the second is the program that can be run after compilation, Windows can run directly.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.