private static Otvet_C_Part Get_C_Part_OneNum(string IdNum, string Patch, MainF.Messenge messenge) { Otvet_C_Part otvet = new Otvet_C_Part(); Patch = Path.Combine(Patch, IdNum); Directory.CreateDirectory(Patch); HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(Function.getHTML(Setting.Url + @"/problem?id=" + IdNum)); // Поиск ответа на странице HtmlNodeCollection htmlNodes = htmlDocument.DocumentNode.SelectNodes("//div"); List <PatchDat> DatFoftos = new List <PatchDat>(0); foreach (var Elem in htmlNodes) { try { if (Elem.Attributes["id"].Value == "sol" + IdNum) { htmlDocument.LoadHtml(Elem.InnerHtml); otvet.Data = Elem.InnerHtml; break; } } catch { } } // Парсин элементов ответа if (otvet.Data != null) { htmlNodes = htmlDocument.DocumentNode.SelectNodes("//img"); WebClient webClient = new WebClient(); webClient.Headers.Add("User-Agent", Setting.UserAgent); webClient.Headers.Add("Cookie", Setting.Cookie); foreach (var Elem in htmlNodes) { restartImg: try { string PatchFileSave = Elem.Attributes["src"].Value; PatchFileSave = PatchFileSave.Remove(0, PatchFileSave.LastIndexOf("/") + 1); PatchFileSave = PatchFileSave.Replace('?', '1').Replace('=', '9'); PatchFileSave = Path.Combine(Patch, PatchFileSave); PatchFileSave += PatchFileSave.Contains(".svg") ? null : ".svg"; string UrlDownload = Elem.Attributes["src"].Value.Contains("http") ? Elem.Attributes["src"].Value : (Setting.Url + Elem.Attributes["src"].Value); webClient.DownloadFile(UrlDownload, PatchFileSave); otvet.Data = otvet.Data.Replace(Elem.Attributes["src"].Value, UrlDownload); DatFoftos.Add(new PatchDat { Url = UrlDownload, PatchFile = PatchFileSave }); } catch { goto restartImg; } } otvet.PatchFotos = DatFoftos.ToArray(); } else { Console.WriteLine("Error"); } return(otvet); }
public static VarOtvet Get(string ID, string Patch, bool C_Part = true, MainF.Messenge messenge = null) { DatVarHTML datVarHTML = GetDataVar(ID); VarOtvet varOtvet = new VarOtvet { Id = ID, otvet_A_Parts = new List <Otvet_A_Part>(0), otvet_C_Parts = new List <Otvet_C_Part>(0) }; HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(Function.PostHtml(Setting.Url + @"/test", datVarHTML.Data)); // Получаем Html двух таблиц HtmlNodeCollection htmlNodes = htmlDocument.DocumentNode.SelectNodes("//div"); string OneTab = null; string TwoTab = null; foreach (var Elem in htmlNodes) { try { if (Elem.Attributes["style"].Value == "vertical-align:top;margin: 5px;text-align:center; display:inline-block; max-width:300px") { TwoTab = Elem.InnerHtml; } else if (Elem.Attributes["style"].Value == "vertical-align:top;margin: 5px;text-align:center; display:inline-block; max-width:500px") { OneTab = Elem.InnerHtml; } else if (OneTab != null && TwoTab != null) { break; } } catch { } } // Парсинг части A htmlDocument.LoadHtml(OneTab); htmlNodes = htmlDocument.DocumentNode.SelectNodes("//tr"); foreach (var Component in htmlNodes) { try { if (Component.Attributes["class"].Value == "res_row") { HtmlDocument html = new HtmlDocument(); html.LoadHtml(Component.InnerHtml); HtmlNodeCollection nodes = html.DocumentNode.SelectNodes("//td"); if (nodes != null) { varOtvet.otvet_A_Parts.Add(new Otvet_A_Part { Num = (uint)(varOtvet.otvet_A_Parts.Count + 1), Resul = nodes[nodes.Count - 1].InnerText }); } } } catch { } } // Парсинг части C Patch = Path.Combine(Patch, varOtvet.Id); Directory.CreateDirectory(Patch); if (C_Part) { htmlDocument.LoadHtml(TwoTab); htmlNodes = htmlDocument.DocumentNode.SelectNodes("//tr"); foreach (var Component in htmlNodes) { if (Component.Attributes["class"]?.Value == "res_row") { HtmlDocument html = new HtmlDocument(); html.LoadHtml(Component.InnerHtml); HtmlNodeCollection nodes = html.DocumentNode.SelectNodes("//td"); if (nodes != null) { var Res = Get_C_Part_OneNum(nodes[1].InnerText, Patch, messenge); Res.Num = (uint)(varOtvet.otvet_C_Parts.Count + 1); varOtvet.otvet_C_Parts.Add(Res); } } } } return(varOtvet); }