Парсинг ссылок из HTML документа - C#

Узнай цену своей работы

Формулировка задачи:

Вот такой код нашел на просторах интернета:
Листинг программы
  1. List links = new List();
  2. foreach (HtmlElement link in webBrowserDocument.Links)
  3. {
  4. string href = link.GetAttribute(“HREF”);
  5. links.Add(href); //добавляем очередную ссылку в лист
  6. }
  7. listBox1.DataSource = links; //отображаем ссылки в ListBox
Но он не работает, помогите

Решение задачи: «Парсинг ссылок из HTML документа»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. using System.Web;
  7. namespace ConsoleApplication2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             WebClient client = new WebClient();
  14.             Stream stream = client.OpenRead("http://www.rambler.ru");
  15.             StreamReader sr = new StreamReader(stream);
  16.             string L,M="";
  17.             while ((L = sr.ReadLine()) != null)
  18.             {
  19.                 M += L;
  20.             }
  21.             stream.Close();
  22.             sr.Close();
  23.            // Console.WriteLine(M);
  24.            getBetween(M, "<a href=\"", "\"");
  25.            
  26.             Console.Read();
  27.         }
  28.  
  29.         private static void getBetween(string strSource, string strStart, string strEnd)
  30.         {
  31.             int Start, End;
  32.            
  33.             while (strSource.Contains(strStart) && strSource.Contains(strEnd))
  34.             {
  35.                 Start = strSource.IndexOf(strStart, 0) + strStart.Length;
  36.                 End = strSource.IndexOf(strEnd, Start);
  37.                 Console.WriteLine(strSource.Substring(Start, End - Start));
  38.                 strSource = strSource.Substring(End);
  39.             }
  40.            
  41.            
  42.         }
  43.     }
  44. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.273 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут