基于C#的百度图片批量下载工具
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工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

C 语言中符号的使用方法涵盖算术、赋值、条件、逻辑、位运算符等。算术运算符用于基本数学运算,赋值运算符用于赋值和加减乘除赋值,条件运算符用于根据条件执行不同操作,逻辑运算符用于逻辑操作,位运算符用于位级操作,特殊常量用于表示空指针、文件结束标记和非数字值。

在 C 语言中,char 类型在字符串中用于:1. 存储单个字符;2. 使用数组表示字符串并以 null 终止符结束;3. 通过字符串操作函数进行操作;4. 从键盘读取或输出字符串。

C语言中通过转义序列处理特殊字符,如:\n表示换行符。\t表示制表符。使用转义序列或字符常量表示特殊字符,如char c = '\n'。注意,反斜杠需要转义两次。不同平台和编译器可能有不同的转义序列,请查阅文档。

在 C 语言中,char 和 wchar_t 的主要区别在于字符编码:char 使用 ASCII 或扩展 ASCII,wchar_t 使用 Unicode;char 占用 1-2 个字节,wchar_t 占用 2-4 个字节;char 适用于英语文本,wchar_t 适用于多语言文本;char 广泛支持,wchar_t 依赖于编译器和操作系统是否支持 Unicode;char 的字符范围受限,wchar_t 的字符范围更大,并使用专门的函数进行算术运算。

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。

在 C 语言中,char 类型转换可以通过:强制类型转换:使用强制类型转换符将一种类型的数据直接转换为另一种类型。自动类型转换:当一种类型的数据可以容纳另一种类型的值时,编译器自动进行转换。

C语言中没有内置求和函数,需自行编写。可通过遍历数组并累加元素实现求和:循环版本:使用for循环和数组长度计算求和。指针版本:使用指针指向数组元素,通过自增指针遍历高效求和。动态分配数组版本:动态分配数组并自行管理内存,确保释放已分配内存以防止内存泄漏。

char 数组在 C 语言中存储字符序列,声明为 char array_name[size]。访问元素通过下标运算符,元素以空终止符 '\0' 结尾,用于表示字符串终点。C 语言提供多种字符串操作函数,如 strlen()、strcpy()、strcat() 和 strcmp()。
