-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrawler.java
186 lines (152 loc) · 5.38 KB
/
Crawler.java
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
package PROJECT;
import java.util.*;
import java.util.Vector;
import org.htmlparser.beans.StringBean;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import java.util.StringTokenizer;
import org.htmlparser.beans.LinkBean;
import java.net.URL;
import java.net.URLConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class Crawler
{
//private String url;
public Crawler()
{
}
public Vector<String> extractWords(String inputurl) throws ParserException
{
Vector<String> v = new Vector<String>();
try {
URL url = new URL(inputurl);
URLConnection uc = url.openConnection();
//get connection via a stringbean
StringBean sb = new StringBean();
//sb.setLinks();
sb.setConnection(uc);
String stringCollected = sb.getStrings();
String[] stringVector = stringCollected.split(" |\\s+|\\n",0);
Collections.addAll(v,stringVector);
} catch (Exception ex) {
System.err.println(ex.toString());
}
return v;
}
public int getPageSize(String inputurl) throws ParserException {
int pageSize = -1;
try {
URL url = new URL(inputurl);
URLConnection uc = url.openConnection();
pageSize = uc.getContentLength();
} catch (Exception ex) {
System.err.println(ex.toString());
}
return pageSize;
}
public long getLastModificationDate(String inputurl) throws ParserException {
long lastModified = -1;
try {
URL url = new URL(inputurl);
URLConnection uc = url.openConnection();
lastModified = uc.getLastModified();
} catch (Exception ex) {
System.err.println(ex.toString());
}
return lastModified;
}
public Vector<String> breadthFirstSearch(int limit, String url) throws ParserException {
Vector<String> result = new Vector<String>();
Queue<String> queue = new LinkedList<String>();
Set<String> visited = new HashSet<String>();
queue.add(url);
visited.add(url);
while (!queue.isEmpty() && result.size() < limit) {
String currentUrl = queue.poll();
result.add(currentUrl);
try {
URLConnection uc = new URL(currentUrl).openConnection();
LinkBean lb = new LinkBean();
lb.setConnection(uc);
String[] links = Arrays.stream(lb.getLinks()).map(URL::toString).toArray(String[]::new);
for (String link : links) {
if (!visited.contains(link)) {
queue.add(link);
visited.add(link);
}
}
} catch (Exception ex) {
System.err.println(ex.toString());
}
}
return result;
}
public Vector<String> extractTitle(String url) throws IOException{
Document doc = Jsoup.connect(url).get();
String[] title = doc.title().split("\\s+");
Vector<String> v = new Vector<String>();
Collections.addAll(v,title);
return v;
}
public Vector<String> extractLinks(String inputurl) throws ParserException
{
// extract links in url and return them
// ADD YOUR CODES HERE
Vector<String> v = new Vector<String>();
try {
URL url = new URL(inputurl);
URLConnection uc = url.openConnection();
//Use LinkBean
LinkBean lb = new LinkBean();
lb.setConnection(uc);
String[] links = Arrays.stream(lb.getLinks()).map(URL::toString).toArray(String[]::new);
Collections.addAll(v,links);
} catch (Exception ex) {
System.err.println(ex.toString());
}
return v;
}
public Vector<String> getThreeHundredLinks(String url) throws ParserException{
int limit = 300; // Specify the number of links to retrieve
Vector<String> bfsLinks = breadthFirstSearch(limit,url);
System.out.println("BFS Links in " + url + " (limit = " + limit + "):");
for (String bfsLink : bfsLinks) {
System.out.println(bfsLink);
}
return bfsLinks;
}
public static void main (String[] args)
{
try
{
Crawler crawler = new Crawler();
Vector<String> words = crawler.extractWords(args[0]);
System.out.println("Words in "+ args[0] + " (size = "+words.size()+") :");
for(int i = 0; i < words.size(); i++)
if(i<5 || i>words.size()-6){
System.out.println(words.get(i));
} else if(i==5){
System.out.println("...");
}
System.out.println("\n\n");
Vector<String> links = crawler.extractLinks(args[0]);
System.out.println("Links in "+ args[0]+":");
for(int i = 0; i < links.size(); i++)
System.out.println(links.get(i));
System.out.println("");
Vector<String> bfsLinks = crawler.getThreeHundredLinks(args[0]);
}
catch (ParserException e)
{
e.printStackTrace ();
}
}
}