Выбор только ссылок из Html - C#
Формулировка задачи:
Подскажите пожалуйста как вытащить только ссылку.
Листинг программы
- <a href="http://kino-teatr.ua/cinema/sputnik-34.phtml" title="Кинотеатр Спутник">Кинотеатр Спутник</a>
Листинг программы
- string cinemaName = "lalala";
- string html = "http://kino-teatr.ua/bill.phtml?city=kiev";
- HtmlDocument htmlDocument = new HtmlDocument();
- var web = new HtmlWeb
- {
- AutoDetectEncoding = false,
- OverrideEncoding = Encoding.UTF8,
- };
- htmlDocument = web.Load(html);
- HtmlNodeCollection NoAltElements = htmlDocument.DocumentNode.SelectNodes("//a[@title]");
- int index = 0;
- string resultSearch = "";
- if (NoAltElements != null)
- {
- foreach (HtmlNode htmlNode in NoAltElements)
- {
- if (htmlNode.Attributes["title"].Value.Contains(cinemaName))
- {
- index++;
- resultSearch += htmlNode.Attributes["title"].Value + "\n";
- }
- }
- }
- if (index > 1)
- {
- MessageBox.Show("Найдено " + index + " совпадения\n" + resultSearch);
- }
- if (index == 0)
- {
- MessageBox.Show("Кинотеатр не найден");
- }
- else
- {
- HtmlNodeCollection NoAltElements2 = htmlDocument.DocumentNode.SelectNodes("//a[@href]//a[@title]");
- if (NoAltElements2 != null)
- {
- foreach (HtmlNode htmlNode in NoAltElements2)
- {
- if (htmlNode.Attributes["title"].Value.Contains(cinemaName))
- {
- resultSearch += htmlNode.Attributes["href"].Value + "\n";// тут нужно вытащить ссылку если имя совпало
- }
- }
- }
- }
- MessageBox.Show(resultSearch);
Разобрался, тему можно закрывать
Листинг программы
- HtmlNodeCollection NoAltElements2 = htmlDocument.DocumentNode.SelectNodes("//span[@id='afishaKtName']//a[@href/@title]");
Решение задачи: «Выбор только ссылок из Html»
textual
Листинг программы
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д