public override void HtmlParser(OnCompleteEventArgs e) { try { lock (_lock) { var stopWatch = new Stopwatch(); stopWatch.Start(); IList <Img> imgs = new List <Img>(); //初始化解析器 var selector = new XPathSelector(e.Page); var aEles = selector.SelectNodes("//*[@id='main']/div/div[1]/a"); foreach (var aEle in aEles) { string url = aEle.GetAttributeValue("href", ""); imgs.Add(new Img() { Url = url }); } _imageService.AddImages(imgs); stopWatch.Stop(); //记录爬取日志 _loggerService.WriteLog(new Log() { DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Msg = e.Uri + "请求消耗:" + e.Duration + "---" + "数据解析消耗:" + stopWatch.ElapsedMilliseconds, ClassName = "", ActionName = "", Duration = e.Duration + stopWatch.ElapsedMilliseconds, LogLevel = (int)LCore.Logger.LogLevel.Info }); } } catch (Exception exception) { //记录错误信息 _loggerService.WriteLog(new Log() { DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), LogLevel = (int)LCore.Logger.LogLevel.Error, ClassName = this.GetType().Name, ActionName = exception.TargetSite.Name, Msg = e.Uri + "---" + exception.Message }); } }
public async Task <IActionResult> AddImage(ImageData imageData, IFormFile file) { if (file == null || file.Length == 0) { return(Content("file not selected")); } var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Images", file.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } imageData.Id = new Guid(); imageData.ImageUrl = file.FileName; imageData.PostedBy = "Vignesh"; imageData.PostedDate = DateTime.Now; imageService.AddImages(imageData); return(RedirectToAction("GetImage")); }