public PageDownloadCompleteEventArgs(Page page) { this.page = page; }
// 保存所有扫描出来的页面到数据库里面 protected void SaveScanResult(object sender, BaseRule.ScanCompleteEventArgs e) { // 同一时间只能一个线程保存数据 System.Threading.Mutex m = new System.Threading.Mutex(false, "SaveDB"); m.WaitOne(); using (UnitOfWork uow = new UnitOfWork(XpoDefault.DataLayer)) // 开始事务 { uow.BeginTransaction(); // 对于每个取得的新页面,判断是否已经存在,如果不存在就保存 foreach (Page page in e.pages) { XPCollection<Page> pages = new XPCollection<Page>(uow, CriteriaOperator.Parse("URL = ?", page.URL)); if (pages.Count() == 0) { Page newPage = new Page(uow) { Parent_ID = CurrentWeb.Oid, Title = page.Title, URL = page.URL }; newPage.Save(); } } uow.CommitChanges(); } m.ReleaseMutex(); }
public string GetPageDate(Page p) { // 如果没有取得Page就退出 if (p.ParentWeb == null) return ""; HtmlDocument doc = WebHelper.GetHtmlDocument(p.URL, p.ParentWeb.Encoding); return GetDataFromContent(doc.DocumentNode.InnerHtml); //HtmlNodeCollection firstpage = doc.DocumentNode.SelectNodes(Page_XPath); //if (firstpage != null && firstpage.Count >= 1) //{ // return firstpage[0].InnerHtml; //} //return ""; }
/// 从列表中取得页面 /// <summary> /// 保存指定列表中的所有的页面数据 /// </summary> /// <param name="web"></param> /// <param name="list_url"></param> /// <returns>正常返回为真,读取网页内容出错,则为假</returns> public List<Page> GetPagesOnList(string list_url) { // 定义一个Session,仅为初始化Page实例而用 Session session = XpoDefault.Session; List<Page> list = new List<Page>(); // 取得列表面的内容 HtmlDocument doc = WebHelper.GetHtmlDocument(list_url); // 如果没有取到内容,返回空列表 if (doc == null) return list; // 分析里面的结点 HtmlNodeCollection lists = doc.DocumentNode.SelectNodes(this.List_XPath); if (lists == null) // 象http://www.avic.com.cn/cn/xwzx/jtxw/index.shtml这个列表的最后一页就是空的。 { return list; } // 取所有符合要求的结点 foreach (HtmlNode node in lists) { // 生成一个新的page实例 Page page = new Page(session); // 取得联接 page.URL = GeneRightURL(node.Attributes["href"].Value); // 网页的标题 if (node.Attributes["title"] != null) page.Title = node.Attributes["title"].Value; else page.Title = node.InnerText; list.Add(page); // 加入查到的页面结点 } return list; }
/// <summary> /// 下载指定网页的内容 /// </summary> /// <param name="p"></param> public string GetPageContent(Page p) { // 如果没有取得Page就退出 if (p.ParentWeb == null) return ""; HtmlDocument doc = WebHelper.GetHtmlDocument(p.URL, p.ParentWeb.Encoding); HtmlNodeCollection firstpage = doc.DocumentNode.SelectNodes(Page_XPath); if (firstpage != null && firstpage.Count >= 1) { return FilterImage(firstpage[0]); } return ""; }