24 Aralık 2015 Perşembe

C# linkleri alma

class Program
{
static void Main(string[] args)
{
// Example usage:
WebClient client = new WebClient();
byte[] buffer = client.DownloadData("http://www.yahoo.jp");
// GetString() extension method is from:
// http://www.shrinkrays.net/code-snippets/csharp/an-extension-method-for-converting-a-byte-array-to-a-string.aspx
string html = buffer.GetString();
List<string> list = LinkExtractor.Extract(html);
foreach (var link in list)
{
Console.WriteLine(link);
}
Console.ReadLine();
}
}
public class LinkExtractor
{
/// <summary>
/// Extracts all src and href links from a HTML string.
/// </summary>
/// <param name="html">The html source</param>
/// <returns>A list of links - these will be all links including javascript ones.</returns>
public static List<string> Extract(string html)
{
List<string> list = new List<string>();
Regex regex = new Regex("(?:href|src)=[\"|']?(.*?)[\"|'|>]+", RegexOptions.Singleline | RegexOptions.CultureInvariant);
if (regex.IsMatch(html))
{
foreach (Match match in regex.Matches(html))
{
list.Add(match.Groups[1].Value);
}
}
return list;
}
}