diff --git a/Classes/ChapterSuduguClass.cs b/Classes/ChapterSuduguClass.cs index b354a3974d192e7ae645a6f6480306878cc6cd34..f383f74e0f92d8d0e3c2116deb674ef764b2897e 100644 --- a/Classes/ChapterSuduguClass.cs +++ b/Classes/ChapterSuduguClass.cs @@ -1,7 +1,7 @@ using HtmlAgilityPack; using HtmlToTxtWFA.Utils; using System.Collections.Generic; -using System.Net.Http; +using System.Text; using System.Windows.Forms; namespace HtmlToTxtWFA.Classes @@ -13,61 +13,86 @@ namespace HtmlToTxtWFA.Classes { public ChapterSuduguClass() { } - public ChapterSuduguClass(string aElement) + public ChapterSuduguClass(HtmlNode aElement) { - // 第一章 礼物 - string[] parts = aElement.Replace("", "").Replace("
  • ", "").Replace("", "").Replace(">", "").Split("\""); + // 第1章 自救者天救之 + this.Label = aElement.InnerText; + this.Url = aElement.GetAttributeValue("href", ""); + } + + public override List ReadChapterList(string HOST, string serialNumber) + { + // https://www.sudugu.com/189/#dir + // https://www.sudugu.com/189/p-2.html#dir + HtmlAgilityPack.HtmlDocument doc = new HtmlWeb().LoadFromWebAsync(HOST + "/" + serialNumber + "/#dir", Encoding.UTF8).Result; + if (doc == null) + { + MessageBox.Show("服务器访问异常"); + return new List(); + } + + HtmlNodeCollection oNodes = doc.DocumentNode.SelectNodes(".//option"); + if (oNodes.Count == 0) + { + MessageBox.Show("获取章节分页异常"); + return new List(); + } - if (parts.Length == 3) + List chapterList = new List(); + for (int i = 0; i < oNodes.Count; i++) { - this.Label = parts[2]; - this.Url = parts[1]; + string url = i == 0 ? HOST + "/" + serialNumber + "/#dir" : HOST + "/" + serialNumber + "/p-" + (i + 1) + ".html#dir"; + chapterList.AddRange(ReadChapterListByPage(url)); } + + return chapterList; } - public override List ReadChapterList(string HOST, string serialNumber) + private List ReadChapterListByPage(string url) { - using (HttpClient client = new HttpClient()) + HtmlAgilityPack.HtmlDocument doc = new HtmlWeb().LoadFromWebAsync(url, Encoding.UTF8).Result; + if (doc == null) { - client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=utf-8"); - HttpResponseMessage response = client.GetAsync(HOST + "/" + serialNumber + "/#dir").Result; - string syncReturn = response.Content.ReadAsStringAsync().Result; - List chapterList = new List(); - if (syncReturn != null) + return new List(); + } + + // 加载章节列表 + HtmlNodeCollection conNodes = doc.DocumentNode.SelectNodes("//div[@id=\"list\"]"); + if (conNodes.Count == 0) + { + return new List(); + } + + HtmlNodeCollection aNodes = conNodes[0].SelectNodes(".//a"); + if (aNodes == null || aNodes.Count == 0) + { + return new List(); + } + + List chapterList = new List(); + for (int c = 0; c < aNodes.Count; c++) + { + // 第1章 自救者天救之 + HtmlNode aNode = aNodes[c]; + if (aNode == null) { - string[] lines = syncReturn.Split("\r\n"); - for (int i = 0; i < lines.Length; i++) - { - //
  • 第一章 礼物... - if (lines[i].Contains("
  • 第") && lines[i].Contains("")) - { - string[] chapters = lines[i].Split("
  • "); - for (int c = 0; c < chapters.Length; c++) - { - // 第一章 礼物 - if (chapters[c].Contains("第") && chapters[c].Contains("")) - { - ChapterSuduguClass chapterClass = new ChapterSuduguClass(chapters[c]); - chapterList.Add(chapterClass); - } - } - break; - } - } + continue; } - else + + ChapterSuduguClass chapterClass = new ChapterSuduguClass(aNode); + if (!chapterClass.IsNull()) { - MessageBox.Show("未查询到章节信息"); + chapterList.Add(chapterClass); } - return chapterList; } + + return chapterList; } public override void ToTxt(string filePath, string HOST) { HtmlWeb web = new HtmlWeb(); HtmlAgilityPack.HtmlDocument doc; - HtmlNode headNode; HtmlNodeCollection conNodes; // 第一页 @@ -78,10 +103,8 @@ namespace HtmlToTxtWFA.Classes return; } - //获得title标签节点,其子标签下的所有节点也在其中 - headNode = doc.DocumentNode.SelectSingleNode("//title"); //写入标题 - TxtUtil.Write(filePath, " " + headNode.InnerText.Replace("小说-全文免费阅读-速读谷", "") + "\n"); + TxtUtil.Write(filePath, " " + this.Label + "\n"); conNodes = doc.DocumentNode.SelectNodes("//*[@class=\"con\"]"); if (conNodes == null || conNodes.Count == 0) {