C#下载小说源码分享
利用.net 下载小说的一个小程序,目的主要是学习,为了不被封,设置了每3秒下载一个章节,目前实现的功能,如果有需要的拿去。本代码利用了HtmlAgilityPack 进行解析,是第三方库,需要NuGet安装或者引用DLL文件
安装HtmlAgilityPack
安装方法网上可以搜到
下面是代码:
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DownLoad88XiaoShuo
{
public partial class FrmMain : Form
{
Thread thread;
public FrmMain() {
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;//线程间操作无效的解决办法
}
/// <summary>
/// 验证是不是正确的书箱地址
/// </summary>
/// <param name="txt">Url</param>
/// <returns>返回是否符合 true or false</returns>
public static bool IsHtml(string txt) {
Regex regex = new Regex(@"^https://www.88xiaoshuo.net/Partlist(/[\d+./]*)$");
return regex.IsMatch(txt);
}
private async Task DownLoadXiaoShuo88(string url) {
btnStartDown.Enabled = false;
string baseUrl = @"https://www.88xiaoshuo.net";
//直接调用获取节点的方法Respose(url)
HtmlNode htmlNode = GetDocument(url).DocumentNode;
string title = htmlNode.SelectSingleNode("//div[@id='info']/a/h1").InnerText;
//判断文件夹是否存在
string downLoadPath = $@"{Environment.CurrentDirectory}\downLoad\{title.Trim()}\";
//如果没有这个目录创建
if (!Directory.Exists(downLoadPath)) {
Directory.CreateDirectory(downLoadPath);
}
HtmlNodeCollection node = htmlNode.SelectNodes("//div[@id='list']/dl/dd/a");
Dictionary<string, string> dict = new Dictionary<string, string>();
int count = 0;
foreach (var item in node) {
count++;
if (count < 13) continue;
try {
dict.Add(item.InnerText, baseUrl + item.Attributes["href"].Value);
//break;
File.AppendAllText(downLoadPath + "/a_dict.txt", $"{item.InnerText.Trim()} ,{baseUrl + item.Attributes["href"].Value}{Environment.NewLine}");
}
catch (ArgumentException) {
File.AppendAllText(downLoadPath + "/a_log.txt", $"章节 {item.InnerText.Trim()}---> 出现重复{Environment.NewLine}");
btnStartDown.Enabled = true;
//throw;
}
}
StringBuilder stringBuilder = new StringBuilder();
int BarStart = 1;//进度条初始数据
prbDownLoad.Maximum = dict.Count; //设置进度条的最大值
foreach (var item in dict) {
prbDownLoad.Value = BarStart++;
string saveFile = $@"{downLoadPath}{item.Key.Trim()}.txt";
if (File.Exists(saveFile)) continue; // 如果文件存在了就跳过
HtmlNodeCollection content = GetDocument(item.Value).DocumentNode.SelectNodes("//div[@id='content']/p");
foreach (var _content in content) {
stringBuilder.Append(_content.InnerText.Trim());
stringBuilder.Append(Environment.NewLine);
}
File.WriteAllText(saveFile, stringBuilder.ToString());
stringBuilder.Clear();
await Task.Delay(3001);
//if (BarStart - 1 != dict.Count) {
// textBox1.AppendText($"第 {BarStart - 1} 章下载完成, 还有 {dict.Count - BarStart + 2} 章没有下载,请耐心等待......{Environment.NewLine}");
//}
//else {
// textBox1.AppendText($"{dict.Count} 章已经全部下载完成{Environment.NewLine}");
//}
}
btnStartDown.Enabled = true;
MessageBox.Show("下载结束......");
}
/// <summary>
/// 获取网页的节点
/// </summary>
/// <param name="url">网址</param>
/// <returns>Node节点</returns>
private HtmlAgilityPack.HtmlDocument GetDocument(string url) {
HtmlWeb htmlWeb = new HtmlWeb();
// 设置请求的User-Agent头,模拟浏览器访问
htmlWeb.PreRequest += handler;
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document = htmlWeb.Load(url);
return document;
}
/// <summary>
/// 设置请求头等相关信息,比如cookieuserAgent
/// </summary>
HtmlWeb.PreRequestHandler handler = delegate (HttpWebRequest request) {
request.CookieContainer = new CookieContainer();
request.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36";
return true;
};
private void btnQuit_Click(object sender, EventArgs e) {
Application.Exit();
}
private void btnStartDown_Click(object sender, EventArgs e) {
string Default_url = @"https://www.88xiaoshuo.net/Partlist/314179/";//梦源头24章节
//string url = @"https://www.88xiaoshuo.net/Partlist/308159/";
//string url = @"https://www.88xiaoshuo.net/Partlist/316967/"; //末世?干自己的19章节
//string url = @"https://www.88xiaoshuo.net/Partlist/44592/";//190章节 猎艳江湖
//115章节 六零年,穿成了心机女主的垫脚石
//string url = @"https://www.88xiaoshuo.net/Partlist/87285/";
//string url = @"https://www.88xiaoshuo.net/Partlist/50408/"; //瑜伽老师花样多 206章节
//string url = @"https://www.88xiaoshuo.net/Partlist/312709/"; //玄幻:再见乔峰 178章节
//利用正则验证网址 如果文本框中的地址正确那么文本框里面的,否则 默认下载地址
string url = IsHtml(txtUrl.Text.Trim()) ? txtUrl.Text.Trim() : Default_url;
thread = new Thread(async () => await DownLoadXiaoShuo88(url));
thread.Start();
// 等待线程结束
thread.Join();
}
}
}
打包源码下载地址:https://luxingyu329.lanzoue.com/i1uuE2605gsd
打包成品下载地址:https://luxingyu329.lanzoue.com/iVd7P2606lab KMT-jiang 发表于 2024-7-31 08:10
请假下咋用??
有成品,小说的网址输入进文件框点开始下载就可以了,然后在软件目录下有一个DownLoad文件夹,里面就是下载的小说章节,文本框中的地址,可以进入网站找到 站内也有个多线程下载小说的,参考下【https://www.52pojie.cn/forum.php?mod=viewthread&tid=1710853&highlight=C%23%2B7.0】 值得学习,这就去下小说() 好好好,有用的 很不错,学习下。 学习了,谢谢楼主分享! 感谢分享 学习了,谢谢楼主。 大佬牛,感谢大佬分享 感谢大佬分享。 感谢分享