/// <summary> /// 搜索网页返回数据 /// </summary> /// <param name="searchBook"></param> /// <param name="searchInfos_Biquge5200Cc"></param> /// <returns></returns> private string Search_Biquge5200Cc(string searchBook, ref List <Object.SearchInfo_Biquge5200Cc> searchInfos_Biquge5200Cc) { string html = string.Empty; HttpWebRequest searchRequest = Request.ResqestHelper.IniRequest("https://www.biquge5200.cc/modules/article/search.php?searchkey=" + searchBook); try { for (int i = 0; i < 5; i++) { html = Request.ResqestHelper.GetUzipResponse(searchRequest); if (!string.IsNullOrWhiteSpace(html)) { break; } i++; } if (string.IsNullOrWhiteSpace(html)) { return("请求失败"); } } catch (Exception e) { labInfo.Visibility = Visibility.Visible; labInfo.Content = e.Message + "\n搜索失败请稍后再试!"; return("请求失败"); } var doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNodeCollection bookInfoNodes = doc.DocumentNode.SelectNodes("//table[@class='grid']/tr"); int num = 0; searchInfos_Biquge5200Cc.Clear(); //2 foreach (HtmlNode node in bookInfoNodes) { if (num > 0) { Object.SearchInfo_Biquge5200Cc info = new Object.SearchInfo_Biquge5200Cc(); info.BookName = node.SelectSingleNode("td[@class='odd']/a").InnerText; info.BookLink = node.SelectSingleNode("td[@class='odd']/a").Attributes["href"].Value; info.LatestChapter = node.SelectSingleNode("td[@class='even']/a").InnerText; info.ChapterLink = node.SelectSingleNode("td[@class='even']/a").Attributes["href"].Value; info.Date = node.SelectNodes("td[@class='odd']")[2].InnerText; info.Auther = node.SelectNodes("td[@class='odd']")[1].InnerText; info.WordCount = node.SelectNodes("td[@class='even']")[1].InnerText; searchInfos_Biquge5200Cc.Add(info); } num++; } #region /////1 //HtmlNodeCollection odd = doc.DocumentNode.SelectNodes("//table[@class='grid']/tr/td[@class='odd']"); //HtmlNodeCollection even = doc.DocumentNode.SelectNodes("//table[@class='grid']/tr/td[@class='even']"); //for (int i= 0; i < bookInfoNodes.Count+1; i++) //{ // info.BookName = odd[i+i*2].ChildNodes[0].InnerText; // info.BookLink = odd[i+i*2].ChildNodes[0].Attributes["href"].Value; // info.LatestChapter = even[i + i * 2].ChildNodes[0].InnerText; // info.ChapterLink = even[i + i * 2].ChildNodes[0].Attributes["href"].Value; // info.Date = odd[i + 2 + i * 2].InnerText; // info.Auther = odd[i + 1 + i * 2].InnerText; // searchInfos_Biquge5200Cc.Add(info); //} ////3 //for(int i= 1; i < bookInfoNodes.Count; i++) //{ // info.BookName = bookInfoNodes[i].SelectSingleNode("td[@class='odd']/a").InnerText; // info.BookLink = bookInfoNodes[i].SelectSingleNode("td[@class='odd']/a").Attributes["href"].Value; // info.LatestChapter = bookInfoNodes[i].SelectSingleNode("td[@class='even']/a").InnerText; // info.ChapterLink = bookInfoNodes[i].SelectSingleNode("td[@class='even']/a").Attributes["href"].Value; // info.Date = bookInfoNodes[i].SelectNodes("td[@class='odd']")[2].InnerText; // info.Auther = bookInfoNodes[i].SelectNodes("td[@class='odd']")[1].InnerText; // searchInfos_Biquge5200Cc.Add(info); // i++; //} #endregion return("ok"); }
public string DownBook() { bookContents.Clear(); Object.SearchInfo_Biquge5200Cc SearchInfo_Biquge5200Cc = DateGridBookInfo.SelectedValue as Object.SearchInfo_Biquge5200Cc; //初始化小说信息 Kindle.BookInfo.BookName = ReplaceBadCharOfFileName(SearchInfo_Biquge5200Cc.BookName); Kindle.BookInfo.Auther = SearchInfo_Biquge5200Cc.Auther; //初始化请求 HttpWebRequest listReq = Request.ResqestHelper.IniRequest(SearchInfo_Biquge5200Cc.BookLink); //获取每章的名字和链接 string listHtml = Request.ResqestHelper.GetUzipResponse(listReq); //下载内容 try { bookContents = GetNextUrl(listHtml); } catch (Exception e) { MessageBox.Show(e.Message + "\n目录获取失败,请稍后再试"); return("error"); } if (bookContents.Count != 0) { //初始化进度条 grid1.Visibility = Visibility.Visible; progressBar1.Maximum = bookContents.Count + 1; progressBar1.Minimum = 0; UpdateProgressBarDelegate updateProgressBaDelegate = new UpdateProgressBarDelegate(progressBar1.SetValue); //获取小说内容 int num = 0; int error = 0; int val = 0; foreach (Kindle.BookContent bT in bookContents) { if (!string.IsNullOrWhiteSpace(bT.Content)) { Dispatcher.Invoke(updateProgressBaDelegate, DispatcherPriority.Background, new object[] { RangeBase.ValueProperty, Convert.ToDouble(num) }); labDown.Content = $"已下载完成: {num}/{bookContents.Count}"; try { bT.Content = GetContent(Request.ResqestHelper.GetUzipResponse(Request.ResqestHelper.IniRequest(bT.Content))); Thread.Sleep(300); } catch { bT.Content = $"<a href=\"{bT.Content}\">{bT.chapter}</a>"; error++; } num++; } if (error > 10 && val == 0) { MessageBoxResult result = MessageBox.Show("获取小说章节失败超过10章,是否结束下载!", "提示", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { return("error"); } val = 1; } } } //显示进度条 return("ok"); }