private async void Worker() { loader = new HtmlLoader(parserSettings); for (int i = parserSettings.StartPoint; i <= parserSettings.EndPoint; i++) { if (!Program.ParserActive) { return; } var source = await loader.GetSourceByPageId(i); var domParser = new HtmlParser(); var document = await domParser.ParseDocumentAsync(source); Parser.Parse(document); lock (locker) OnParseOneLink?.Invoke(); } if (Program.ParserActive) { ParseComplete?.Invoke(); } }
public async void Work() { const string url = "https://citydog.by/allposts/category/people/"; var loader = new HtmlLoader(url); var source = await loader.GetSourceByPageId(1); var parser = new HtmlParser(); var document = parser.Parse(source); var itemsText = document.QuerySelectorAll( "div > div.headingArticle > h2 > a"); var itemsImg = document.QuerySelectorAll( "div > div.imageWrapper > a > img"); var listText = itemsText.Select(item => item.TextContent).ToList(); var listImg = itemsImg.Select(item => item.GetAttribute("src")).ToList(); var dic = listText.Zip(listImg, (k, v) => new { k, v }) .ToDictionary(x => x.k, x => x.v); foreach (var res in dic) { Console.WriteLine($"Заголовок: {res.Key}"); Console.WriteLine($"Ссылка на изображение: {res.Value}"); Console.WriteLine($"-----------------------------------------------------------"); } }
private async void Worker() { while (isActive) { var source = await loader.GetSourceByPageId(id); // скачиваем html код страницы // в виде строки var domParser = new HtmlParser(); var result = source as T; // приводим строку к T // (в данном случае это необязательно) OnNewData?.Invoke(this, result); // вызываем событие OnNewData for (int i = 0; i < 60; i++) // задержка { if (!isActive) { break; } await Task.Delay(500); } } OnCompleted?.Invoke(this); // вызываем событие OnCompleted }
public async Task CanParse() { // Arrange IParserSettings parserSettings = new ShopSettings(1, 1); HtmlLoader loader = new HtmlLoader(parserSettings); ShopParser parser = new ShopParser(); var domParser = new HtmlParser(); var source = await loader.GetSourceByPageId(2); var document = await domParser.ParseAsync(source); // Act var result = parser.Parse(document); //Assert Assert.IsNotNull(parser.PricesList); }
private async void Worker() { for (int i = parserSettings.StartPoint; i <= parserSettings.EndPoint; i++) { if (!isActive) { OnComplited?.Invoke(this); return; } var source = await loader.GetSourceByPageId(i); var domParser = new HtmlParser(); var document = await domParser.ParseAsync(source); var result = parser.Parse(document); OnNewData?.Invoke(this, result); } OnComplited?.Invoke(this); isActive = false; }
public async Task <StatisticsList> Handle(GetStatisticListVm request, CancellationToken cancellationToken) { if (context_.Statistics.Any(x => x.DateTime.Date == DateTime.Now.Date)) { var statistic = await context_.Statistics.ToListAsync(cancellationToken); return(StatisticsList(statistic.ToList())); } else { loader_ = new HtmlLoader <StatisticDto_>(settings); var source_ = await loader_.GetSourceByPageId(); CQ cq = CQ.Create(source_); var result = parser_.Parse(cq).ToList(); //for (int i = 0; i < result.Count; i++) //{ // var republic = context_.Republics.Find(result[i].Republic); // if (republic!=null) // { // await context_.Statistics.AddAsync(new Statistic() // { // DateTime = DateTime.Now, // CountInfected = result[i].CountInfected, // Dead = result[i].Dead, // Recovered = result[i].Recovered, // RepublicId = republic.Title, // VirusId = context_.Viruses.Find("Covid").Id // }); // await context_.SaveChanges(cancellationToken); // } // //} return(new StatisticsList { statisticList = result.ToList() }); } }
private async void Worker() { for (int i = Config.StartPoint; i <= Config.EndPoint; i++) { if (!isWorking) { OnEndAction(this); return; } var source = await loader.GetSourceByPageId(i); var localParser = new HtmlParser(); var document = await localParser.ParseDocumentAsync(source); var result = Parser.Parse(document); OnIterationAction?.Invoke(this, result); } OnEndAction?.Invoke(this); isWorking = false; }
public async Task <RepublicsViewModel> Handle(GetRepublicsListQuery request, CancellationToken cancellationToken) { if (!context_.Republics.Any()) { HtmlLoader_ = new HtmlLoader <Republic>(this.settings_); var source_ = await HtmlLoader_.GetSourceByPageId(); CQ cq = CQ.Create(source_); var result = parser_.Parse(cq).ToArray(); for (int i = 0; i < result.Count(); i++) { context_.Republics.Add(result[i]); await context_.SaveChanges(cancellationToken); } return(new RepublicsViewModel() { Republics = result.Select(x => new RepublicDto() { Population = x.Population, Square = x.Square, Title = x.Title }).ToList() }); } else { var republics = await context_.Republics.ToListAsync(cancellationToken); return(new RepublicsViewModel() { Republics = republics.Select(x => new RepublicDto() { Population = x.Population, Square = x.Square, Title = x.Title }).ToList() }); } }