All questions
MediumGraphs · Breadth-First Search
Web Crawler Same Host
Written by a community member · · 4 attempts · 0% pass
2
You are given a starting URL start_url and the pages of an in-memory web: pages[i] is a URL and links[i] lists the URLs found on that page. A URL that is not in pages has no links.
Implement a web crawler that returns all URLs reachable from start_url that share the same hostname as start_url. The order of URLs in the result does not matter.
Your function:
def crawl(start_url: str, pages: list[str], links: list[list[str]]) -> list[str]:
pass
Requirements:
- Start from
start_url. - Avoid duplicates: never crawl the same URL twice.
- Restrict scope: only follow URLs whose hostname matches that of
start_url. - Assume all URLs use the
httpprotocol and do not include a port. - Do not require URL normalization beyond exact string matching.
- Treat URL fragments as part of the URL string; if they appear, they are distinct URLs unless they are exact duplicates.
Example 1:
Input:
start_url = "http://news.example.com/"pages = ["http://news.example.com/", "http://news.example.com/a", "http://news.example.com/b"]links = [["http://news.example.com/a", "http://news.example.com/b", "http://other.com/x"], ["http://news.example.com/b"], []]
Output: ["http://news.example.com/", "http://news.example.com/a", "http://news.example.com/b"]
Example 2:
Input:
start_url = "http://example.com/index"pages = ["http://example.com/index", "http://example.com/index#top", "http://example.com/about"]links = [["http://example.com/index#top", "http://example.com/about"], [], []]
Output: ["http://example.com/index", "http://example.com/index#top", "http://example.com/about"]