Given a url startUrl and an interface HtmlParser, implement a web crawler to crawl all links that are under the same hostname as startUrl.
Return all urls obtained by your web crawler in any order.
Your crawler should:
Start from the page: startUrl
Call HtmlParser.getUrls(url) to get all urls from a webpage of given url.
Do not crawl the same link twice.
Explore only the links that are under the same hostname as startUrl.
As shown in the example url above, the hostname is example.org. For simplicity sake, you may assume all urls use http protocol without any
port specified. For example, the urls http://leetcode.com/problems and
http://leetcode.com/contest are under the same hostname, while urls
http://example.org/test and http://example.com/abc are not under the same hostname.
The HtmlParser interface is defined as such:
interface HtmlParser { // Return a list of all urls from a webpage of given _url_.
public List<String> getUrls(String url); }
Below are two examples explaining the functionality of the problem, for custom testing purposes you’ll have three variables urls, edges and startUrl.
Notice that you will only have access to startUrl in your code, while urls
and edges are not directly accessible to you in code.
****
Input:
urls =["http://news.yahoo.com","http://news.yahoo.com/news","http://news.yahoo.com/news/topics/","http://news.google.com"]edges =[[0,2],[2,1],[3,2],[3,1],[3,0]]startUrl ="http://news.google.com"Output: ["http://news.google.com"]Explanation: The startUrl links to all other pages that do not share the same hostname.
Constraints:
1 <= urls.length <= 1000
1 <= urls[i].length <= 300
startUrl is one of the urls.
Hostname label must be from 1 to 63 characters long, including the dots, may contain only the ASCII letters from ‘a’ to ‘z’, digits from ‘0’ to ‘9’ and the hyphen-minus character (’-’).
The hostname may not start or end with the hyphen-minus character (’-’).
We need to crawl all URLs reachable from startUrl, but only those with the same hostname. We use BFS or DFS, keeping a set of visited URLs, and only add URLs with the same hostname as startUrl.
// interface HtmlParser { fun getUrls(url: String): List<String> }
classSolution {
funcrawl(startUrl: String, htmlParser: HtmlParser): List<String> {
fungetHost(url: String): String {
val p = url.indexOf('/', 7)
return url.substring(7, if (p == -1) url.length else p)
}
val host = getHost(startUrl)
val vis = mutableSetOf(startUrl)
val q = ArrayDeque<String>()
q.add(startUrl)
while (q.isNotEmpty()) {
val u = q.removeFirst()
for (v in htmlParser.getUrls(u)) {
if (v !in vis && getHost(v) == host) {
vis.add(v)
q.add(v)
}
}
}
return vis.toList()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from typing import List
# HtmlParser is provided by the systemclassSolution:
defcrawl(self, startUrl: str, htmlParser: 'HtmlParser') -> List[str]:
defget_host(url: str) -> str:
p = url.find('/', 7)
return url[7:] if p ==-1else url[7:p]
host = get_host(startUrl)
vis = set([startUrl])
q = [startUrl]
while q:
u = q.pop(0)
for v in htmlParser.getUrls(u):
if v notin vis and get_host(v) == host:
vis.add(v)
q.append(v)
return list(vis)