/// <summary> /// 写入页面缓存到文件 /// </summary> /// <param name="bookClasses">所有大分类</param> public void WriteCache() { //更新所有的更改 tygdb.SaveChanges(); //得到当前大分类的复本 var bookClasses = ClassesCache.ToList(); //开始并行化写入文件 bookClasses.ForEach((o) => { //如果有过修改则需要重新写入 //重得到一个复本,这样在下面的查询中就不会出现集合在别的位置修改后出错了 var books = o.Value.Books.ToList(); //得到所有需要修改的记录 //指定在查询要执行大量非计算绑定工作(如文件 I/O)的情况下,最好指定比计算机上的核心数大的并行度。 这里指定的是2个核心 var list = books.AsParallel().WithDegreeOfParallelism(2).Where(p => p.Value.Modifyd); //对搜索到需要更新的记录进行更新. list.ForAll((p) => { var pages = p.Value.Pages.ToList(); //一本书用一个线程 pages.Where(x => x.Value.Modifyd).ToList().ForEach((x) => { //更新页面记录 UpdateRecord(x.Value); }); //更新书本缓存记录 UpdateRecord(p.Value); }); }); }
protected void Page_Load(object sender, EventArgs e) { //初始化一个DOM // HtmlAgilityPack.HtmlDocument dom = new HtmlAgilityPack.HtmlDocument(); // dom.LoadHtml("http://www.86zw.com/Book/33839/Index.aspx".GetWeb()); //更新所有文章的子章节状态 0 表示没有有效的章节 TygModel.Entities enti = new TygModel.Entities(); var dd = enti.书名表.ToList(); dd = dd.Where(p => p.包含有效章节 == null || p.包含有效章节 == 0).ToList(); for (int k = 0; k < dd.Count(); k++) { dd.ElementAt(k).包含有效章节 = dd.ElementAt(k).文章表.Count; enti.SaveChanges(); } }
protected void Page_Load(object sender, EventArgs e) { // -- 去除重复记录 //--delete from [书名表] where ID not in(select max(ID) from [书名表] group by [书名] having count([书名])>=1); //更新排序 new Skybot.Tong.TongUse().GetWeb("http://localhost/百度风云榜/小说前50.aspx"); try { ///////////////////////////////////开始进行数采集 //表示5个任务同时开始 Skybot.Cache.RecordsCacheManager.Instance.Tygdb.书名表 .OrderByDescending(p => p.最后更新时间) //.Where(p => p.书名 == "吞噬星空" || p.书名 == "仙逆") .Take(50).AsParallel().WithDegreeOfParallelism(15).ForAll((o) => { using (TygModel.Entities entities = new TygModel.Entities()) { var books = entities.书名表.Where(p => p.GUID == o.GUID); if (books.Count() > 0) { GetContents(books.First(), entities); o.最后更新时间 = DateTime.Now; //保存记录数 不允许启动新事务,因为有其他线程正在该会话中运行。 entities.SaveChanges(); } } }); //打印状态 System.Diagnostics.Debug.WriteLine("开始采集 所有书本信息"); } catch (Exception) { } return; try { ///////////////////////////////////开始进行数采集 //表示5个任务同时开始 Skybot.Cache.RecordsCacheManager.Instance.Tygdb.书名表 .Where(p => p.文章表.Count < 0) //.Where(p => p.书名 == "吞噬星空" || p.书名 == "仙逆") .Take(9999).AsParallel().WithDegreeOfParallelism(15).ForAll((o) => { using (TygModel.Entities entities = new TygModel.Entities()) { var books = entities.书名表.Where(p => p.GUID == o.GUID); if (books.Count() > 0) { GetContents(books.First(), entities); //保存记录数 不允许启动新事务,因为有其他线程正在该会话中运行。 entities.SaveChanges(); } } }); } catch (Exception) { } //保存数 Skybot.Cache.RecordsCacheManager.Instance.Tygdb.SaveChanges(); }
/// <summary> /// 转换数据 /// </summary> /// <returns></returns> public override TygModel.书名表 Convert() { DateTime updateTime; DateTime.TryParse(更新, out updateTime); TygModel.书名表 book = null; //看看分类表里有没有这个分类如果没有则进行分类添加 using (TygModel.Entities tygdb = new TygModel.Entities()) { //分类表 var classItems = tygdb.分类表.Where(p => p.分类名称.Trim() == 类别.Trim()); //当前分类 TygModel.分类表 classItem = null; //如果分类不存在 if (classItems.Count() == 0) { classItem = new TygModel.分类表() { 分类标识 = 类别.Trim(), 分类名称 = 类别.Trim(), 分类说明 = 类别.Trim(), 备注 = "来自 yankuaikan.com", 通用分类 = 类别.Trim() }; tygdb.AddTo分类表(classItem); //保存分类 tygdb.SaveChanges(); } else { classItem = classItems.FirstOrDefault(); } book = new TygModel.书名表() { 分类表 = classItem, 分类标识 = classItem.分类标识, 分类表ID = classItem.ID, GUID = Guid.NewGuid(), 采集用的URL1 = 小说目录URL, 采集用的URL2 = 小说简介URL, 创建时间 = DateTime.Now, 最新章节 = 最新章节, 作者名称 = 作者, 说明 = "", 书名 = 小说名称.Replace("》", "").Replace("《", ""), 最后更新时间 = updateTime, 完本 = 状态.Trim() == "完成" ? true : false, 配图 = new Func <string>(() => { if (小说简介URL != null) { //初始化一个DOM HtmlAgilityPack.HtmlDocument dom = new HtmlAgilityPack.HtmlDocument(); dom.LoadHtml(小说简介URL.GetWeb()); //内容 HtmlAgilityPack.HtmlNode listContent = dom.GetElementbyId("content"); //可能的原素 List <PossiblyResultElement> possiblyResultElements = new List <PossiblyResultElement>(); //开始循环子原素 SingleListPageAnalyse.AnalyseMaxATagNearest(listContent, possiblyResultElements, 0, new PossiblyResultElement() { ParentPossiblyResult = null, CurrnetHtmlElement = listContent, LayerIndex = -1, ContainTagNum = 0 }); //计算当前所有HTML原素中的img原素 var PageimgElements = from img in possiblyResultElements where img.CurrnetHtmlElement.Name == "a" && img.CurrnetHtmlElement.HasChildNodes && img.CurrnetHtmlElement.ChildNodes.Where(p => p.Name == "img").Count() > 0 select img; try { if (PageimgElements.Count() > 0) { //img.CurrnetHtmlElement.Attributes["src"].Value.Contains("http://tu.yankuai.com") && img.CurrnetHtmlElement.Attributes["src"] != null string imgurl = PageimgElements.First().CurrnetHtmlElement.ChildNodes[0].Attributes["src"].Value; return(imgurl.Trim().Contains("http://tu.yankuai.com") ? imgurl : "/images/noimg.jpg"); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(DateTime.Now + ex.Message + "|||||" + ex.StackTrace); } } //找到文章目录 return("/images/noimg.jpg"); }).Invoke(), }; } return(book); }
/// <summary> /// 转换数据 /// </summary> /// <returns></returns> public override TygModel.书名表 Convert() { DateTime updateTime = DateTime.Parse(更新); //书本 TygModel.书名表 book = null; //看看分类表里有没有这个分类如果没有则进行分类添加 using (TygModel.Entities tygdb = new TygModel.Entities()) { //分类表 var classItems = tygdb.分类表.Where(p => p.分类名称.Trim() == 类别.Trim()); //当前分类 TygModel.分类表 classItem = null; //如果分类不存在 if (classItems.Count() == 0) { try { classItem = new TygModel.分类表() { 分类标识 = 类别.Trim(), 分类名称 = 类别.Trim(), 分类说明 = 类别.Trim(), 备注 = "来自 www.xs52.com", 通用分类 = 类别.Trim() }; tygdb.AddTo分类表(classItem); //保存分类 tygdb.SaveChanges(); } catch (Exception ex) { System.Diagnostics.UDPGroup.SendStrGB2312(ex.Message + (ex.StackTrace != null ? ex.StackTrace : "")); } } else { classItem = classItems.FirstOrDefault(); } book = new TygModel.书名表() { 分类标识 = classItem.分类标识, 分类表ID = classItem.ID, GUID = Guid.NewGuid(), 采集用的URL1 = 小说目录URL, 采集用的URL2 = 小说简介URL, 创建时间 = DateTime.Now, 最新章节 = 最新章节, 作者名称 = 作者, 说明 = "", 书名 = 小说名称.Replace("》", "").Replace("《", ""), 最后更新时间 = updateTime, 完本 = 状态.Trim() == "连载" ? false : true, 配图 = "/images/noimg.gif", }; //修改配图或者说明 if (小说简介URL != null) { book.说明 = string.Format("小说《{0}》{1}/著", 小说名称, 作者); } } return(book); }
protected void Page_Load(object sender, EventArgs e) { // -- --update [Tyg].[dbo].[书名表] set 采集用的URL1='http://www.86zw.com/Html/Book/32/32600/Index.shtml' where [书名]='永生' //书名集合 List <string> BookNames = new List <string>(); HtmlAgilityPack.HtmlDocument dom = new HtmlAgilityPack.HtmlDocument(); dom.LoadHtml("http://top.baidu.com/buzz.php?p=book".GetWeb()); //可能的原素 List <PossiblyResultElement> possiblyResultElements = new List <PossiblyResultElement>(); //开始循环子原素 SingleListPageAnalyse.AnalyseMaxATagNearest(dom.DocumentNode, possiblyResultElements, 0, new PossiblyResultElement() { ParentPossiblyResult = null, CurrnetHtmlElement = dom.DocumentNode, LayerIndex = -1, ContainTagNum = 0 }); //计算当前所有HTML原素中的tr原素 var PageTrElements = from tr in possiblyResultElements where tr.CurrnetHtmlElement.Name == "tr" select tr; //填类 foreach (var item in PageTrElements) { if (item.CurrnetHtmlElement.HasChildNodes) { var els = item.CurrnetHtmlElement.ChildNodes.Where(p => p.HasChildNodes); double x; if (els.ElementAt(0).Name == "th" && double.TryParse(els.ElementAt(0).InnerText, out x)) { BookNames.Add(els.ElementAt(1).InnerText); } } } TygModel.Entities tntity = new TygModel.Entities(); tntity.CommandTimeout = 60 * 100; //找到前50的小说并对数据库记录进行更新 //更新所有小说的书名 foreach (var k in tntity.书名表) { if (BookNames.Contains(k.书名.Trim())) { k.最后更新时间 = DateTime.Now; } //《时空玄仙》 // k.书名 = k.书名.Replace("》", "").Replace("《", ""); } //提交更新 tntity.SaveChanges(); tntity.Dispose(); }
protected void Page_Load(object sender, EventArgs e) { Response.Write( Server.UrlDecode("http://www.trimedgas.com/error/error.aspx?Error='s'+%e9%99%84%e8%bf%91%e6%9c%89%e8%af%ad%e6%b3%95%e9%94%99%e8%af%af%e3%80%82%0d%0a%e5%85%b3%e9%94%ae%e5%ad%97+'with'+%e9%99%84%e8%bf%91%e6%9c%89%e8%af%ad%e6%b3%95%e9%94%99%e8%af%af%e3%80%82%e5%a6%82%e6%9e%9c%e6%ad%a4%e8%af%ad%e5%8f%a5%e6%98%af%e5%85%ac%e7%94%a8%e8%a1%a8%e8%a1%a8%e8%be%be%e5%bc%8f%e6%88%96+xmlnamespaces+%e5%ad%90%e5%8f%a5%ef%bc%8c%e9%82%a3%e4%b9%88%e5%89%8d%e4%b8%80%e4%b8%aa%e8%af%ad%e5%8f%a5%e5%bf%85%e9%a1%bb%e4%bb%a5%e5%88%86%e5%8f%b7%e7%bb%93%e5%b0%be%e3%80%82%0d%0a%e5%85%b3%e9%94%ae%e5%ad%97+'with'+%e9%99%84%e8%bf%91%e6%9c%89%e8%af%ad%e6%b3%95%e9%94%99%e8%af%af%e3%80%82%e5%a6%82%e6%9e%9c%e6%ad%a4%e8%af%ad%e5%8f%a5%e6%98%af%e5%85%ac%e7%94%a8%e8%a1%a8%e8%a1%a8%e8%be%be%e5%bc%8f%e6%88%96+xmlnamespaces+%e5%ad%90%e5%8f%a5%ef%bc%8c%e9%82%a3%e4%b9%88%e5%89%8d%e4%b8%80%e4%b8%aa%e8%af%ad%e5%8f%a5%e5%bf%85%e9%a1%bb%e4%bb%a5%e5%88%86%e5%8f%b7%e7%bb%93%e5%b0%be%e3%80%82%0d%0a%e5%85%b3%e9%94%ae%e5%ad%97+'with'+%e9%99%84%e8%bf%91%e6%9c%89%e8%af%ad%e6%b3%95%e9%94%99%e8%af%af%e3%80%82%e5%a6%82%e6%9e%9c%e6%ad%a4%e8%af%ad%e5%8f%a5%e6%98%af%e5%85%ac%e7%94%a8%e8%a1%a8%e8%a1%a8%e8%be%be%e5%bc%8f%e6%88%96+xmlnamespaces+%e5%ad%90%e5%8f%a5%ef%bc%8c%e9%82%a3%e4%b9%88%e5%89%8d%e4%b8%80%e4%b8%aa%e8%af%ad%e5%8f%a5%e5%bf%85%e9%a1%bb%e4%bb%a5%e5%88%86%e5%8f%b7%e7%bb%93%e5%b0%be%e3%80%82" ) ); var books = Skybot.Cache.RecordsCacheManager.Instance.Tygdb.书名表.ToList(); for (int classid = 1; classid <= 8; classid++) { System.Diagnostics.Debug.WriteLine("开始采集图书列表" + "http://www.86zw.com/Book/ShowBookList.aspx?tclassid=" + classid + "&page={0}"); var al = new Skybot.Collections.Sites.BookList86zw_com() { BaseUrl = "http://www.86zw.com/Book/ShowBookList.aspx?tclassid=" + classid + "&page={0}" }.DoWork(); //开始写入数据库 var AllBooks = from doc in al select doc; //添加到Book中 for (int k = 0; k < AllBooks.Count(); k++) { try { TygModel.书名表 book = AllBooks.ElementAt(k).Convert(); //当前记录 var records = books.Where(p => p.书名.Replace("》", "").Replace("《", "").Trim() == book.书名.Replace("》", "").Replace("《", "").Trim() && p.作者名称.Trim() == book.作者名称.Trim()); if (records.Count() > 0) { //得到当前书更新记录 TygModel.书名表 currentBook = records.FirstOrDefault(); // currentBook.分类表 = book.分类表; //currentBook.分类表ID = book.分类表ID; //currentBook.GUID = book.GUID; currentBook.采集用的URL1 = book.采集用的URL1; currentBook.采集用的URL2 = book.采集用的URL2; // currentBook.创建时间 = book.创建时间; currentBook.最新章节 = book.最新章节; //currentBook.作者名称 = book.作者名称; currentBook.说明 = book.说明; // currentBook.书名 = book.书名; currentBook.最后更新时间 = book.最后更新时间; currentBook.完本 = book.完本; currentBook.配图 = book.配图; //保存更改 Skybot.Cache.RecordsCacheManager.Instance.Tygdb.SaveChanges(); } else { Skybot.Cache.RecordsCacheManager.Instance.Tygdb.AddTo书名表(book); Skybot.Cache.RecordsCacheManager.Instance.Tygdb.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message + (ex.StackTrace != null ? ex.StackTrace : "")); } System.Diagnostics.Debug.WriteLine("已经完成书" + k + "/" + AllBooks.Count()); } } System.Diagnostics.Debug.WriteLine("86zw 所有图书状态更新完成"); //更新书有效章节数量 TygModel.Entities enti = new TygModel.Entities(); var dd = enti.书名表.ToList(); dd = dd.Where(p => p.包含有效章节 == null || p.包含有效章节 == 0).ToList(); for (int k = 0; k < dd.Count(); k++) { dd.ElementAt(k).包含有效章节 = dd.ElementAt(k).文章表.Count; enti.SaveChanges(); } }
/// <summary> /// 转换数据 /// </summary> /// <returns></returns> public override TygModel.书名表 Convert() { DateTime updateTime = DateTime.Parse("2000-01-01"); //书本 TygModel.书名表 book = null; //看看分类表里有没有这个分类如果没有则进行分类添加 using (TygModel.Entities tygdb = new TygModel.Entities()) { //分类表 var classItems = tygdb.分类表.Where(p => p.分类名称.Trim() == 类别.Trim()); //当前分类 TygModel.分类表 classItem = null; //如果分类不存在 if (classItems.Count() == 0) { try { classItem = new TygModel.分类表() { 分类标识 = 类别.Trim(), 分类名称 = 类别.Trim(), 分类说明 = 类别.Trim(), 备注 = "来自 86zw.com", 通用分类 = 类别.Trim() }; tygdb.AddTo分类表(classItem); //保存分类 tygdb.SaveChanges(); } catch (Exception ex) { System.Diagnostics.UDPGroup.SendStrGB2312(ex.Message + (ex.StackTrace != null ? ex.StackTrace : "")); } } else { classItem = classItems.FirstOrDefault(); } book = new TygModel.书名表() { 分类表 = classItem, 分类标识 = classItem.分类标识, 分类表ID = classItem.ID, GUID = Guid.NewGuid(), 采集用的URL1 = 小说目录URL, 采集用的URL2 = 小说简介URL, 创建时间 = DateTime.Now, 最新章节 = 最新章节, 作者名称 = 作者, 说明 = "", 书名 = 小说名称.Replace("》", "").Replace("《", ""), 最后更新时间 = updateTime, 完本 = 状态.Trim() == "完结" ? true : false, 配图 = "/images/noimg.gif", }; //修改配图或者说明 if (小说简介URL != null) { System.Diagnostics.UDPGroup.SendStrGB2312("获取配图:" + 小说简介URL); //初始化一个DOM HtmlAgilityPack.HtmlDocument dom = new HtmlAgilityPack.HtmlDocument(); dom.LoadHtml(小说简介URL.GetWeb()); //信息说明字段 HtmlAgilityPack.HtmlNode DesriptionContent = dom.GetElementbyId("CrbtrTop"); //采集时间的xpath表达式 2011-11-18 HtmlAgilityPack.HtmlNode node = dom.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/div[2]/div[3]/div[2]/div[1]/ul[1]/li[6]"); if (node != null) { if (DateTime.TryParse(node.InnerText, out updateTime)) { book.最后更新时间 = updateTime; } } //说明 HtmlAgilityPack.HtmlNode summary = dom.GetElementbyId("CrbsSum"); if (summary != null) { book.说明 = summary.InnerHtml.Length > 4000 ? new Tong.TongUse().ForMatText(summary.InnerText, 0, 3800) : summary.InnerHtml; } //图片 HtmlAgilityPack.HtmlNode listContent = dom.GetElementbyId("CrbtlBookImg"); if (listContent != null) { //可能的原素 List <PossiblyResultElement> possiblyResultElements = new List <PossiblyResultElement>(); //开始循环子原素 SingleListPageAnalyse.AnalyseMaxATagNearest(listContent, possiblyResultElements, 0, new PossiblyResultElement() { ParentPossiblyResult = null, CurrnetHtmlElement = listContent, LayerIndex = -1, ContainTagNum = 0 }); //计算当前所有HTML原素中的img原素 var PageimgElements = from img in possiblyResultElements where img.CurrnetHtmlElement.Name == "img" select img; try { if (PageimgElements.Count() > 0) { string imgurl = PageimgElements.First().CurrnetHtmlElement.Attributes["src"].Value; if (!imgurl.ToLower().Contains("images/noimg.gif")) { try { imgurl = new Uri(new Uri(小说简介URL), imgurl).ToString(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(DateTime.Now + ex.Message + "|||||" + ex.StackTrace); imgurl = "/images/noimg.gif"; } } System.Diagnostics.UDPGroup.SendStrGB2312("获取配图:" + 小说简介URL + " 完成" + imgurl); book.配图 = imgurl.Trim().Contains("/images/noimg.gif") ? "/images/noimg.gif" : imgurl; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(DateTime.Now + ex.Message + "|||||" + ex.StackTrace); } } } } return(book); }
void 获取中文更新() { //书名集合 List <string> BookNames = new List <string>(); HtmlAgilityPack.HtmlDocument dom = new HtmlAgilityPack.HtmlDocument(); // dom.LoadHtml("http://www.xkzw.org/xkph_2.htm".GetWeb()); dom.LoadHtml(listdiv.InnerHtml); //可能的原素 List <PossiblyResultElement> possiblyResultElements = new List <PossiblyResultElement>(); //开始循环子原素 SingleListPageAnalyse.AnalyseMaxATagNearest(dom.DocumentNode, possiblyResultElements, 0, new PossiblyResultElement() { ParentPossiblyResult = null, CurrnetHtmlElement = dom.DocumentNode, LayerIndex = -1, ContainTagNum = 0 }); //移除第一个 ul 原素 var removeitem = from title in possiblyResultElements where title.CurrnetHtmlElement.Name == "li" select title; //移除 possiblyResultElements.Remove(removeitem.ElementAt(0)); //计算当前所有HTML原素中的tr原素 var PageTrElements = from tr in possiblyResultElements where tr.CurrnetHtmlElement.Name == "li" select tr; List <Skybot.Collections.Sites.BookInfo86zw_com> list = new List <Skybot.Collections.Sites.BookInfo86zw_com>(); using (TygModel.Entities tygdb = new TygModel.Entities()) { var books = tygdb.书名表.ToLookup(p => p.书名.Replace("》", "").Replace("《", "").Trim() + "|" + p.作者名称); //填类 foreach (var item in PageTrElements) { if (item.CurrnetHtmlElement.HasChildNodes) { //span class="fl">[东方玄幻]</span> //<span class="sm"><a href="/xkzw3226/" target="_blank"> //一等家丁</a></span> //<span class="zj"><a href="/xkzw3226/5162956.html" title="第一五七三章 药水" //target="_blank">第一五七三章 药水</a></span> //<span class="zz">纯情犀利哥</span> <span class="zs"> //1608193</span> //<span class="sj">2013-05-12</span> <span class="zt">连载</span> var els = item.CurrnetHtmlElement.SelectNodes("span"); Skybot.Collections.Sites.BookInfo86zw_com bookITEM = new Skybot.Collections.Sites.BookInfo86zw_com(); bookITEM.类别 = els[0].InnerText.Replace("[", "").Replace("]", "").Trim(); bookITEM.小说名称 = els[1].Element("a").InnerText.Replace("\r\n", "").Trim(); bookITEM.小说目录URL = "http://www.xkzw.org/" + els[1].Element("a").Attributes["href"].Value; bookITEM.最新章节 = els[2].InnerText; bookITEM.作者 = els[3].InnerText; bookITEM.更新 = DateTime.Now.ToString(); bookITEM.采集URL = bookITEM.小说目录URL; bookITEM.状态 = els[6].InnerText; bookITEM.小说简介URL = null; list.Add(bookITEM); } } //更新或者是添加书 foreach (var item in list) { string key = item.小说名称 + "|" + item.作者; var query = tygdb.书名表.Where(p => p.书名.Replace("》", "").Replace("《", "").Trim() + "|" + p.作者名称 == key); if (query.Count() > 0) { foreach (var bookItem in query) { bookItem.最后更新时间 = DateTime.Now; } } else { var bok = item.Convert(); //添加记录 Skybot.Cache.RecordsCacheManager.Instance.Tygdb.AddTo书名表(bok); } } tygdb.SaveChanges(); tygdb.Connection.Close(); tygdb.Dispose(); Skybot.Cache.RecordsCacheManager.Instance.Tygdb.SaveChanges(); } }