-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crawler.cs
274 lines (234 loc) · 9.72 KB
/
Crawler.cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection.Metadata.Ecma335;
using System.Threading.Tasks;
using AngleSharp;
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using Newtonsoft.Json;
using WebScraper.Models;
namespace WebScraper
{
public class Crawler
{
private static readonly IEnumerable<string> ExludedSchemas = new[]
{
"mailto",
"tel",
"script"
};
private readonly List<PageObject> pageObjects;
private readonly Queue<CrawlRequest> queue;
private readonly List<TreeNode> rootNodes;
private readonly IBrowsingContext context;
private readonly Options options;
public Crawler(Options opts, IBrowsingContext context, List<PageObject> pageObjects)
{
this.context = context;
this.pageObjects = pageObjects;
options = opts;
queue = new Queue<CrawlRequest>();
rootNodes = new List<TreeNode>();
foreach (var url in opts.StartUrls)
{
queue.Enqueue(new CrawlRequest(new Uri(url), null));
}
}
public void Crawl()
{
var visitedUrls = new HashSet<Uri>();
var stopwatch = Stopwatch.StartNew();
stopwatch.Start();
var scrapedLinkCount = 0;
while (queue.Count != 0)
{
var request = queue.Dequeue();
if (visitedUrls.Contains(request.Url))
continue;
visitedUrls.Add(request.Url);
var result = GetCrawlResult(request.Url);
if (result == null)
continue;
foreach (var pageObj in pageObjects)
{
if (pageObj.TestCondition == null)
continue;
if (pageObj.TestCondition != null && !pageObj.TestCondition(result))
continue;
var node = new TreeNode(pageObj.PageName, result.RequestUrl);
lock (this.rootNodes)
{
if (request.ParentNode == null)
{
rootNodes.Add(node);
}
else
{
if (request.ParentNode.Languages.ContainsKey(result.Language))
{
request.ParentNode.Languages[result.Language] = node;
}
else
{
request.ParentNode.ChildNodes.Add(node);
}
node.Parent = request.ParentNode;
}
}
ScrapeWebsite(result, pageObj, node);
Console.WriteLine("Scraped " + request.Url.AbsoluteUri + " <- " + pageObj.PageName);
scrapedLinkCount++;
break;
}
}
stopwatch.Stop();
Console.WriteLine($"Scraped {scrapedLinkCount} links. Scraping time: {stopwatch.Elapsed}");
SavePropertiesToFile(options.OutputPath);
}
public void SavePropertiesToFile(string path)
{
lock (this.rootNodes)
{
var serializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented
};
var serializedJson = JsonConvert.SerializeObject(rootNodes, serializerSettings);
using (var writer = File.CreateText(path))
{
writer.Write(serializedJson);
writer.Flush();
}
}
}
private void ScrapeWebsite(CrawlResult crawlResult, PageObject pageObject, TreeNode node)
{
ExtractProperties(crawlResult, pageObject, node);
lock (this.rootNodes)
{
if (rootNodes.Contains(node) || !node.IsLanguageSubPage())
{
ExtractLanguages(crawlResult, pageObject, node);
ExtractLinksToQueue(crawlResult, pageObject, node);
ExtractPaginationPages(crawlResult, pageObject, node);
}
}
}
private void ExtractPaginationPages(CrawlResult crawlResult, PageObject pageObject, TreeNode parent)
{
if (string.IsNullOrWhiteSpace(pageObject.Pagination))
return;
Console.WriteLine(crawlResult.RequestUrl + " pagination:");
var currentDoc = crawlResult.Document;
var nextPageRef = currentDoc.QuerySelector(pageObject.Pagination)?.GetAttribute(AttributeNames.Href);
while (nextPageRef != null)
{
var result = GetCrawlResult(BuildUri(crawlResult.RequestUrl, nextPageRef));
if (result == null)
continue;
Console.WriteLine("Extracting links from " + result.RequestUrl);
ExtractLinksToQueue(result, pageObject, parent);
currentDoc = result.Document;
nextPageRef = currentDoc.QuerySelector(pageObject.Pagination)?.GetAttribute(AttributeNames.Href);
}
}
private void ExtractLanguages(CrawlResult crawlResult, PageObject pageObject, TreeNode parent)
{
if (pageObject.Languages.Count == 0)
return;
foreach (var (lang, selector) in pageObject.Languages.Select(x => (x.Key, x.Value)))
{
parent.Languages.Add(lang, null);
var link = crawlResult.Document.QuerySelector(selector)?.GetAttribute(AttributeNames.Href);
if (link != null)
queue.Enqueue(new CrawlRequest(BuildUri(crawlResult.RequestUrl, link), parent));
}
}
private void ExtractLinksToQueue(CrawlResult crawlResult, PageObject pageObject, TreeNode parent)
{
if (pageObject.PageLinks.Count == 0)
return;
foreach (var (linkName, selector) in pageObject.PageLinks.Select(x => (x.Key, x.Value)))
{
var extractedLinks = crawlResult.Document.QuerySelectorAll(selector);
if(extractedLinks == null)
continue;
var links = extractedLinks.Select(l => l.GetAttribute(AttributeNames.Href))
.Where(h => !string.IsNullOrWhiteSpace(h))
.Where(h => ExludedSchemas.All(s => !h.Contains(s)))
.Select(href => BuildUri(crawlResult.RequestUrl, href))
//.Where(uri => !ExludedSchemas.Contains(uri.Scheme, StringComparer.OrdinalIgnoreCase))
.ToList();
links.ForEach(uri => queue.Enqueue(new CrawlRequest(uri, parent)));
}
}
private void ExtractProperties(CrawlResult crawlResult, PageObject pageObject, TreeNode node)
{
if (pageObject.Properties.Count == 0)
return;
foreach (var (propertyName, propertyObject) in pageObject.Properties.Select(x => (x.Key, x.Value)))
{
var elements = crawlResult.Document.QuerySelectorAll(propertyObject.Selector);
if(elements.Length == 0)
continue;
var extractorResult = propertyObject.ExtractProperties(crawlResult, elements);
node.Properties.Add(propertyName, extractorResult);
}
}
private CrawlResult GetCrawlResult(Uri url)
{
var response = GetResponse(url)?.Result;
if (response == null || !response.IsSuccessStatusCode)
return null;
var document = this.ParseResponse(response).Result;
return new CrawlResult(url, document);
}
private static Uri BuildUri(Uri baseUrl, string href)
{
try
{
return new Uri(baseUrl, new Uri(href, UriKind.RelativeOrAbsolute));
}
catch (Exception e)
{
throw new FormatException($"Invalid URI format: '{href}'.", e);
}
}
private async Task<IHtmlDocument> ParseResponse(HttpResponseMessage responseMessage)
{
if (!responseMessage.IsSuccessStatusCode)
return null;
using (var contentStream = await responseMessage.Content.ReadAsStreamAsync())
{
var parser = new HtmlParser(new HtmlParserOptions(), context);
return await parser.ParseDocumentAsync(contentStream);
}
}
private async Task<HttpResponseMessage> GetResponse(Uri url)
{
var httpRequestMessage = new HttpRequestMessage
{
RequestUri = url,
Method = HttpMethod.Get,
};
try
{
using (var client = new HttpClient())
{
return await client.SendAsync(httpRequestMessage);
}
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
}
}