This repository has been archived by the owner on Jul 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrawler.cs
148 lines (126 loc) · 4.66 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace WebCrawler {
class Crawler {
private const int BFS_MAX_DEPTH = 2;
private const int DFS_MAX_DEPTH = 1000;
private static LinkedList<String> urlsToVisit;
private static HashSet<String> urlsVisited;
private static StreamWriter sw;
private static String baseUrl;
public Crawler(String urlpath, int searchtype) {
urlsToVisit = new LinkedList<String>();
urlsVisited = new HashSet<String>();
sw = new StreamWriter("out.txt");
baseUrl = urlpath;
addUrl(urlpath);
sw.WriteLine(urlsVisited.Count() + " " + urlsToVisit.Count() + " " + urlpath + '\n');
if (searchtype == 0) {
crawlBFS();
} else if (searchtype == 1) {
crawlDFS();
}
sw.Close();
}
private static void addUrl(String urlpath) {
urlsToVisit.AddLast(urlpath);
urlsVisited.Add(urlpath);
}
private static bool queueEmpty() {
return urlsToVisit.Count == 0;
}
private static void crawlBFS() {
for (int i = 1; i < BFS_MAX_DEPTH; ++i) {
sw.WriteLine("BFS LEVEL " + i + '\n');
LinkedList<String> newUrl = new LinkedList<String>();
foreach (String urlpath in urlsToVisit) {
String htmlText = getHtmlText(urlpath);
try {
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlText);
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a[@href]");
foreach (HtmlNode node in nodes) {
String url = node.Attributes[0].Value;
if (url.Contains(".pdf")) {
continue;
} else if (!url.Contains("http")) {
if (baseUrl[baseUrl.Length - 1] == '/' && url[0] == '/') {
url = baseUrl.Substring(0, baseUrl.Length - 1) + url;
} else if (baseUrl[baseUrl.Length - 1] == '/' || url[0] == '/') {
url = baseUrl + url;
} else {
url = baseUrl + "/" + url;
}
}
if (url[url.Length - 1] != '/') {
url += '/';
}
if (!urlsVisited.Contains(url)) {
newUrl.AddLast(url);
urlsVisited.Add(url);
sw.WriteLine(urlsVisited.Count() + " " + newUrl.Count() + " " + urlpath + '\n' + url + '\n');
} else {
sw.WriteLine(">>>>> DUPLIKASI " + urlsVisited.Count() + " " + newUrl.Count() + " " + urlpath + '\n' + url + '\n');
}
/*Regex regex = new Regex("<a[^>]+href\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
//Regex regex = new Regex(@"<" + "a" + @"[^>]*?HREF\s*=\s*[""']?([^'"" >]+?)[ '""]?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//Regex regex = new Regex("\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))");
MatchCollection matches = regex.Matches(htmlText);
//Console.WriteLine(matches.Count);
foreach (Match match in matches) {
//Regex avalue = new Regex("(\\S+)=[\"\']?((?:.(?![\"\']?\\s+(?:\\S+)=|[>\"\']))+.)[\"\']?");
//Match m = avalue.Match(match.Value);
String url = match.Value;
Console.WriteLine(url);
Regex value = new Regex("<a[^>]+href\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
if (url.Contains("http")) {
Console.WriteLine("masuk 1");
url = url.Substring(url.IndexOf("href") + 6, url.LastIndexOf(">") - (url.IndexOf("href") + 7));
} else if (url.Contains("://")) {
Console.WriteLine("masuk 2");
url = url.Substring(url.IndexOf("href") + 6, url.LastIndexOf(">") - (url.IndexOf("href") + 7));
} else {
Console.WriteLine("masuk 3");
url = url.Substring(url.IndexOf("href") + 6, url.LastIndexOf(">") - (url.IndexOf("href") + 7));
if (url[0] != '/' && urlpath[urlpath.Length - 1] != '/') {
url = urlpath + "/" + url;
} else {
url = urlpath + url;
}
}
Regex ignore = new Regex("([^\\s]+(\\.(?i)(jpg|png|gif|bmp|doc|docx|ppt|pptx|pdf|wmv))$)");*/
}
} catch (Exception e) {
}
}
urlsToVisit = newUrl;
}
}
private static void crawlDFS() {
for (int i = 0; i < DFS_MAX_DEPTH; ++i) {
//String htmlText = getHtmlText(urlpath);
}
}
private static String getHtmlText(String urlpath) {
try {
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(urlpath);
request.UserAgent = "Web Crawler";
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
String htmlText = reader.ReadToEnd();
//Console.WriteLine(htmlText);
return htmlText;
} catch (Exception e) {
}
return null;
}
}
}