Парсинг ссылок из HTML документа - C#
Формулировка задачи:
Вот такой код нашел на просторах интернета:
Но он не работает, помогите
List links = new List();
foreach (HtmlElement link in webBrowserDocument.Links)
{
string href = link.GetAttribute(“HREF”);
links.Add(href); //добавляем очередную ссылку в лист
}
listBox1.DataSource = links; //отображаем ссылки в ListBoxРешение задачи: «Парсинг ссылок из HTML документа»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://www.rambler.ru");
StreamReader sr = new StreamReader(stream);
string L,M="";
while ((L = sr.ReadLine()) != null)
{
M += L;
}
stream.Close();
sr.Close();
// Console.WriteLine(M);
getBetween(M, "<a href=\"", "\"");
Console.Read();
}
private static void getBetween(string strSource, string strStart, string strEnd)
{
int Start, End;
while (strSource.Contains(strStart) && strSource.Contains(strEnd))
{
Start = strSource.IndexOf(strStart, 0) + strStart.Length;
End = strSource.IndexOf(strEnd, Start);
Console.WriteLine(strSource.Substring(Start, End - Start));
strSource = strSource.Substring(End);
}
}
}
}