-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
68 lines (62 loc) · 2.13 KB
/
Copy pathProgram.cs
File metadata and controls
68 lines (62 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace Crawler
{
class Program
{
private const string BaseUrl = "http://www.microsoftstore.com";
private const string StartUrl = "/store/msca/en_CA/DisplayHelpPage/";
private const int CrawlDepth = 1;
private static List<string> IgnoreUrls = new List<string> { "javascript" };
static void Main(string[] args)
{
RunAsync(StartUrl, CrawlDepth).Wait();
}
static async Task RunAsync(string url, int depth)
{
if (depth < 0)
{
return;
}
else if (IgnoreUrls.Find(u => url.Contains(u)) != null)
{
Console.WriteLine(string.Format("SKIPPED\t{0}", url));
return;
}
Console.WriteLine(string.Format("VALID\t{0}", url));
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(BaseUrl);
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var product = await response.Content.ReadAsStringAsync();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(product);
var links = doc.DocumentNode.SelectNodes("//a[@href]");
if (links == null)
{
return;
}
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
HtmlAttribute att = link.Attributes["href"];
await RunAsync(att.Value, depth - 1);
}
}
else
{
Console.WriteLine("ERROR: {0}", url);
}
}
}
}
}