Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7a448d0

Browse files
authored
Create Web Crawler.java
1 parent2ab62d1 commit7a448d0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎Medium/Web Crawler.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* // This is the HtmlParser's API interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* interface HtmlParser {
5+
* public List<String> getUrls(String url) {}
6+
* }
7+
*/
8+
classSolution {
9+
publicList<String>crawl(StringstartUrl,HtmlParserhtmlParser) {
10+
Set<String>visited =newHashSet<>();
11+
Stack<String>stack =newStack<>();
12+
stack.push(startUrl);
13+
Stringhostname =getHostname(startUrl);
14+
while(!stack.isEmpty()) {
15+
Stringpopped =stack.pop();
16+
visited.add(popped);
17+
List<String>connectedUrls =htmlParser.getUrls(popped);
18+
for (Stringurl :connectedUrls) {
19+
if (!visited.contains(url) &&url.contains(hostname)) {
20+
stack.push(url);
21+
}
22+
}
23+
}
24+
returnnewArrayList<>(visited);
25+
}
26+
27+
privateStringgetHostname(Stringurl) {
28+
String[]splits =url.split("/");
29+
returnsplits[2];
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp