Web Crawler Problem
Description
LeetCode Problem 1236.
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.
Note: Consider the same URL with the trailing slash "/" as a different URL. For example, "http://news.yahoo.com", and "http://news.yahoo.com/" are different urls.
Example 1:

Input:
urls = [
"http://news.yahoo.com",
"http://news.yahoo.com/news",
"http://news.yahoo.com/news/topics/",
"http://news.google.com",
"http://news.yahoo.com/us"
]
edges = [[2,0],[2,1],[3,2],[3,1],[0,4]]
startUrl = "http://news.yahoo.com/news/topics/"
Output: [
"http://news.yahoo.com",
"http://news.yahoo.com/news",
"http://news.yahoo.com/news/topics/",
"http://news.yahoo.com/us"
]
Example 2:

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 <= 10001 <= urls[i].length <= 300startUrlis one of theurls.- 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 ('-').
- See: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_hostnames
- You may assume there're no duplicates in url library.
Solution
Approach: Breadth-First Search on One Hostname
The pages and their links form a directed graph, and we need every page reachable from startUrl through pages on the same hostname. A breadth-first search does that:
- Take the hostname of
startUrl: every url starts with"http://"(7 characters), and the hostname runs from there up to the next"/"or the end. - Put
startUrlin a queue and in aseenset. - Pop a url, call
getUrlson it, and for each link that is not seen yet and has the same hostname, mark it seen and queue it.
Marking a url as seen when it is queued, not when it is popped, means each page is fetched once, even when many pages link to it or the links form a cycle.
Compare whole hostnames, not prefixes of the url: "http://a.com.cn/x" starts with "http://a.com" but is on another host. A page on the start host that is only linked from another host is not crawled, because we never fetch pages on other hosts.
Complexity: O(N + E) calls and set operations for N urls and E links, times the url length L for hashing and comparing strings. O(N · L) space for the set and the queue.
from collections import deque
class Solution:
def crawl(self, startUrl: str, htmlParser: 'HtmlParser') -> List[str]:
def hostname(url):
# "http://" is 7 characters; the hostname runs up to the next "/"
return url[7:].split("/")[0]
host = hostname(startUrl)
seen = {startUrl}
queue = deque([startUrl])
while queue:
url = queue.popleft()
for nxt in htmlParser.getUrls(url):
if nxt not in seen and hostname(nxt) == host:
seen.add(nxt)
queue.append(nxt)
return list(seen)