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.
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.
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 (’-’).
You may assume there’re no duplicates in url library.
class HtmlParser: # Test-harness API: backs the getUrls interface with the url library def __init__(self, urls: list[str], edges: list[list[int]]) -> None: self.adj: dict[str, list[str]] = {u: [] for u in urls} for a, b in edges: self.adj[urls[a]].append(urls[b]) def get_urls(self, url: str) -> list[str]: return self.adj[url]class Solution: # Time: O(V + E) pages and links visited once # Space: O(V) visited set def crawl(self, start_url: str, html_parser: HtmlParser) -> list[str]: host = start_url[7:].split("/")[0] visited = {start_url} stack = [start_url] while stack: url = stack.pop() for link in html_parser.get_urls(url): if link in visited or link[7:].split("/")[0] != host: continue visited.add(link) stack.append(link) return list(visited)
{"docsConfig":{"theme":"mint","$schema":"https://www.mintlify.com/docs.json","name":"leetcode-py","description":"Modern Python LeetCode practice environment with automated problem generation, beautiful data structure visualizations, and comprehensive testing","colors":{"primary":"#3776AB","light":"#5A94C4","dark":"#387EB8"},"logo":{"light":"https://mintcdn.com/leetcode-py/aJKJ4BOUnXP-zM3G/img/brand/leetcode-py-lockup.svg?fit=max\u0026auto=format\u0026n=aJKJ4BOUnXP-zM3G\u0026q=85\u0026s=34c74a404ed256642a54345a92489c97","dark":"https://mintcdn.com/leetcode-py/aJKJ4BOUnXP-zM3G/img/brand/leetcode-py-lockup-dark.svg?fit=max\u0026auto=format\u0026n=aJKJ4BOUnXP-zM3G\u0026q=85\u0026s=263c5757fbf5e547de96291651e279e9"},"favicon":"https://mintcdn.com/leetcode-py/VCrzlUI648LnF7Pk/favicon.svg?fit=max\u0026auto=format\u0026n=VCrzlUI648LnF7Pk\u0026q=85\u0026s=3b1dffe162e80798a504a810fcbeba45","background":{"color":{"light":"#F8F9FB","dark":"#0D1117"}},"navbar":{"links":[{"href":"https://github.com/wislertt/leetcode-py","label":"GitHub"}]},"footer":{"socials":{"github":"https://github.com/wislertt/leetcode-py"},"links":[{"header":"Resources","items":[{"label":"PyPI","href":"https://pypi.org/project/leetcode-py-sdk"},{"label":"Why leetcode-py","href":"/getting-started/why-leetcode-py"},{"label":"Made by Wisaroot Lertthaweedech","href":"https://wisl.dev"}]}]},"seo":{"metatags":{"canonical":"https://leetcode-py.wisl.dev","og:image":"https://leetcode-py.wisl.dev/img/brand/og-card-light.png","twitter:card":"summary_large_image"},"organization":{"url":"https://leetcode-py.wisl.dev","sameAs":["https://github.com/wislertt/leetcode-py","https://pypi.org/project/leetcode-py-sdk/","https://wisl.dev"]},"indexing":"all"},"styling":{"codeblocks":{"theme":{"light":"github-light","dark":"github-dark"}},"latex":true},"metadata":{"timestamp":true},"markdown":{"instructions":["leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.","Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.","Examples are backed by tests; copy them verbatim."]},"navigation":{"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"leetcode-py","hasAudioNarration":false,"actualSubdomain":"leetcode-py","customDomains":["leetcode-py.wisl.dev"],"gitSource":{"type":"github","owner":"wislertt","repo":"leetcode-py","deployBranch":"main","contentDirectory":"docs","isPrivate":true,"mountPath":"","label":""},"entitlements":{},"buildId":"6aeadd835fa1f5f231996c95","clientVersion":"0.0.4094","searchFilterCounts":[{"language":"default","tag":"Catalog","count":13},{"language":"default","tag":"Practice","count":5},{"language":"default","tag":"Troubleshooting","count":5},{"language":"default","tag":"Getting Started","count":3},{"language":"default","tag":"CLI","count":2},{"language":"default","tag":"Contributing","count":2},{"language":"default","tag":"Welcome","count":1}]},"assistantConfig":{"enableAskAiButton":false,"enableStarterQuestions":false,"enableContextualStarterQuestions":false,"enableAskAiSearchBar":true},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-onboarding","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"assistant-v5","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-sso-scim","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-signup","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-enabled","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"sso-setup-deadline-banner","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"comment-mode","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"googleFontsInlined":false},"customCss":[{"filePath":"custom.css","content":".sd-catalog-table th:first-child,.sd-catalog-table td:first-child{min-width:0;width:3rem}html.dark .sd-hero-light{display:none}html:not(.dark) .sd-hero-dark{display:none}.sd-badges{display:flex;flex-wrap:wrap;gap:8px;align-items:center;justify-content:flex-start;margin:0 0 16px;padding:0}.sd-badges a,.sd-badges a:hover,.sd-badges a:visited{display:inline-flex;flex:0 0 auto;line-height:0;height:auto!important;max-height:24px;padding:0!important;margin:0!important;text-decoration:none!important;border-bottom:none!important;background:none!important;background-image:none!important}.sd-badges img{height:20px;width:auto;max-width:none;display:block;margin:0!important}.sd-preview{border:1px solid #387eb8;border-radius:10px}"}],"page":{"pagePath":"problems/web-crawler","slug":"/problems/web-crawler","pageMetadata":{"title":"Web Crawler Python Solution with Tests","description":"Tested Python solution for LeetCode 1236 with 18 pytest cases. Generate a practice environment with lcpy.","href":"/problems/web-crawler"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Tested Python solution for LeetCode 1236 with 18 pytest cases. Generate a practice environment with lcpy.\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,img:`img`,li:`li`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,tbody:`tbody`,td:`td`,th:`th`,thead:`thead`,tr:`tr`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Heading,Table}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Table)_missingMdxReference(`Table`,true);return _jsxs(_Fragment,{children:[_jsxs(_components.p,{children:[`LeetCode 1236, `,_jsx(_components.a,{href:`/catalog/medium`,children:`Medium`}),`. Topics: `,_jsx(_components.a,{href:`/catalog/topics/depth-first-search`,children:`Depth-First Search`}),`, `,_jsx(_components.a,{href:`/catalog/topics/breadth-first-search`,children:`Breadth-First Search`}),`, `,_jsx(_components.a,{href:`/catalog/topics/string`,children:`String`}),`, `,_jsx(_components.a,{href:`/catalog/topics/interactive`,children:`Interactive`}),`. `,_jsx(_components.a,{href:`https://leetcode.com/problems/web-crawler/description/`,children:`View on LeetCode`}),`.`]}),`\n`,_jsxs(_components.p,{children:[`Generate this problem as a practice environment: tested reference solution, 18 `,_jsx(_components.a,{href:`/practice/testing`,children:`parametrized pytest cases`}),`, and a playground notebook:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`shellscript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light github-dark`,style:{backgroundColor:`#fff`,\"--shiki-dark-bg\":`#24292e`,color:`#24292e`,\"--shiki-dark\":`#e1e4e8`},language:`shellscript`,children:_jsxs(_components.code,{language:`shellscript`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#6F42C1`,\"--shiki-dark\":`#B392F0`},children:`lcpy`}),_jsx(_components.span,{style:{color:`#032F62`,\"--shiki-dark\":`#9ECBFF`},children:` gen`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:` -n`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:` 1236`}),_jsx(_components.span,{style:{color:`#6A737D`,\"--shiki-dark\":`#6A737D`},children:` # by problem number`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#6F42C1`,\"--shiki-dark\":`#B392F0`},children:`lcpy`}),_jsx(_components.span,{style:{color:`#032F62`,\"--shiki-dark\":`#9ECBFF`},children:` gen`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:` -s`}),_jsx(_components.span,{style:{color:`#032F62`,\"--shiki-dark\":`#9ECBFF`},children:` web_crawler`}),_jsx(_components.span,{style:{color:`#6A737D`,\"--shiki-dark\":`#6A737D`},children:` # by problem name`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`problem`,children:`Problem`}),`\n`,_jsxs(_components.p,{children:[`Given a url `,_jsx(_components.code,{children:`startUrl`}),` and an interface `,_jsx(_components.code,{children:`HtmlParser`}),`, implement a web crawler to crawl all links that are under the same hostname as `,_jsx(_components.code,{children:`startUrl`}),`.`]}),`\n`,_jsxs(_components.p,{children:[`Return all urls obtained by your web crawler in `,_jsx(_components.strong,{children:`any`}),` order.`]}),`\n`,_jsx(_components.p,{children:`Your crawler should:`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[`Start from the page: `,_jsx(_components.code,{children:`startUrl`})]}),`\n`,_jsxs(_components.li,{children:[`Call `,_jsx(_components.code,{children:`HtmlParser.getUrls(url)`}),` to get all urls from a webpage of given url.`]}),`\n`,_jsx(_components.li,{children:`Do not crawl the same link twice.`}),`\n`,_jsxs(_components.li,{children:[`Explore only the links that are under the same hostname as `,_jsx(_components.code,{children:`startUrl`}),`.`]}),`\n`]}),`\n`,_jsx(_components.p,{children:_jsx(_components.img,{src:`https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1236.Web%20Crawler/images/urlhostname.png`,alt:`Hostname`})}),`\n`,_jsxs(_components.p,{children:[`As shown in the example url above, the hostname is `,_jsx(_components.code,{children:`example.org`}),`. For simplicity sake, you may assume all urls use http protocol without any port specified. For example, the urls `,_jsx(_components.code,{children:`http://leetcode.com/problems`}),` and `,_jsx(_components.code,{children:`http://leetcode.com/contest`}),` are under the same hostname, while urls `,_jsx(_components.code,{children:`http://example.org/test`}),` and `,_jsx(_components.code,{children:`http://example.com/abc`}),` are not under the same hostname.`]}),`\n`,_jsxs(_components.p,{children:[`The `,_jsx(_components.code,{children:`HtmlParser`}),` interface is defined as such:`]}),`\n`,_jsx(CodeBlock,{numberOfLines:`5`,language:`text`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light github-dark`,style:{backgroundColor:`#fff`,\"--shiki-dark-bg\":`#24292e`,color:`#24292e`,\"--shiki-dark\":`#e1e4e8`},language:`text`,children:_jsxs(_components.code,{language:`text`,numberOfLines:`5`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`interface HtmlParser {`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` // Return a list of all urls from a webpage of given url.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` public List\u003cString> getUrls(String url);`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`}`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{})})]})})}),`\n`,_jsxs(_components.p,{children:[`Below are two examples explaining the functionality of the problem, for custom testing purposes you’ll have three variables `,_jsx(_components.code,{children:`urls`}),`, `,_jsx(_components.code,{children:`edges`}),` and `,_jsx(_components.code,{children:`startUrl`}),`. Notice that you will only have access to `,_jsx(_components.code,{children:`startUrl`}),` in your code, while `,_jsx(_components.code,{children:`urls`}),` and `,_jsx(_components.code,{children:`edges`}),` are not directly accessible to you in code.`]}),`\n`,_jsxs(_components.p,{children:[`Note: Consider the same URL with the trailing slash `,_jsx(_components.code,{children:`/`}),` as a different URL. For example, `,_jsx(_components.code,{children:`http://news.yahoo.com`}),`, and `,_jsx(_components.code,{children:`http://news.yahoo.com/`}),` are different urls.`]}),`\n`,_jsx(Heading,{level:`3`,id:`examples`,children:`Examples`}),`\n`,_jsx(_components.p,{children:_jsx(_components.img,{src:`https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1236.Web%20Crawler/images/sample_2_1497.png`,alt:`Example 1`})}),`\n`,_jsx(CodeBlock,{numberOfLines:`18`,language:`text`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light github-dark`,style:{backgroundColor:`#fff`,\"--shiki-dark-bg\":`#24292e`,color:`#24292e`,\"--shiki-dark\":`#e1e4e8`},language:`text`,children:_jsxs(_components.code,{language:`text`,numberOfLines:`18`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`Input:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`urls = [`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com/news\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com/news/topics/\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.google.com\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com/us\"`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`]`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`edges = [[2,0],[2,1],[3,2],[3,1],[0,4]]`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`startUrl = \"http://news.yahoo.com/news/topics/\"`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`Output:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`[`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com/news\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com/news/topics/\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com/us\"`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`]`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{})})]})})}),`\n`,_jsx(_components.p,{children:_jsx(_components.img,{src:`https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1236.Web%20Crawler/images/sample_3_1497.png`,alt:`Example 2`})}),`\n`,_jsx(CodeBlock,{numberOfLines:`12`,language:`text`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light github-dark`,style:{backgroundColor:`#fff`,\"--shiki-dark-bg\":`#24292e`,color:`#24292e`,\"--shiki-dark\":`#e1e4e8`},language:`text`,children:_jsxs(_components.code,{language:`text`,numberOfLines:`12`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`Input:`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`urls = [`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com/news\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.yahoo.com/news/topics/\",`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:` \"http://news.google.com\"`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`]`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`edges = [[0,2],[2,1],[3,2],[3,1],[3,0]]`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`startUrl = \"http://news.google.com\"`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`Output: [\"http://news.google.com\"]`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`Explanation: The startUrl links to all other pages that do not share the same hostname.`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{})})]})})}),`\n`,_jsx(Heading,{level:`3`,id:`constraints`,children:`Constraints`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:_jsx(_components.code,{children:`1 \u003c= urls.length \u003c= 1000`})}),`\n`,_jsx(_components.li,{children:_jsx(_components.code,{children:`1 \u003c= urls[i].length \u003c= 300`})}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.code,{children:`startUrl`}),` is one of the `,_jsx(_components.code,{children:`urls`}),`.`]}),`\n`,_jsx(_components.li,{children:`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 (’-’).`}),`\n`,_jsx(_components.li,{children:`The hostname may not start or end with the hyphen-minus character (’-’).`}),`\n`,_jsx(_components.li,{children:`You may assume there’re no duplicates in url library.`}),`\n`]}),`\n`,_jsx(Heading,{level:`2`,id:`solution`,children:`Solution`}),`\n`,_jsxs(_components.p,{children:[`Reference implementation from `,_jsx(_components.a,{href:`https://github.com/wislertt/leetcode-py/blob/main/leetcode/web_crawler/solution.py`,children:`solution.py on GitHub`}),`, full suite in `,_jsx(_components.a,{href:`https://github.com/wislertt/leetcode-py/blob/main/leetcode/web_crawler/test_solution.py`,children:`test_solution.py`}),`:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`26`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light github-dark`,style:{backgroundColor:`#fff`,\"--shiki-dark-bg\":`#24292e`,color:`#24292e`,\"--shiki-dark\":`#e1e4e8`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`26`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`class`}),_jsx(_components.span,{style:{color:`#6F42C1`,\"--shiki-dark\":`#B392F0`},children:` HtmlParser`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`:`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6A737D`,\"--shiki-dark\":`#6A737D`},children:` # Test-harness API: backs the getUrls interface with the url library`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` def`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:` __init__`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`(self, urls: list[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`str`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`], edges: list[list[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`int`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`]]) -> `}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`None`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:` self`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`.adj: dict[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`str`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`, list[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`str`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`]] `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`=`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` {u: [] `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`for`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` u `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`in`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` urls}`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` for`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` a, b `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`in`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` edges:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:` self`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`.adj[urls[a]].append(urls[b])`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` def`}),_jsx(_components.span,{style:{color:`#6F42C1`,\"--shiki-dark\":`#B392F0`},children:` get_urls`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`(self, url: `}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`str`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`) -> list[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`str`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`]:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` return`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:` self`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`.adj[url]`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`class`}),_jsx(_components.span,{style:{color:`#6F42C1`,\"--shiki-dark\":`#B392F0`},children:` Solution`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`:`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6A737D`,\"--shiki-dark\":`#6A737D`},children:` # Time: O(V + E) pages and links visited once`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6A737D`,\"--shiki-dark\":`#6A737D`},children:` # Space: O(V) visited set`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` def`}),_jsx(_components.span,{style:{color:`#6F42C1`,\"--shiki-dark\":`#B392F0`},children:` crawl`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`(self, start_url: `}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`str`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`, html_parser: HtmlParser) -> list[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`str`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`]:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` host `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`=`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` start_url[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`7`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`:].split(`}),_jsx(_components.span,{style:{color:`#032F62`,\"--shiki-dark\":`#9ECBFF`},children:`\"/\"`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`)[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`0`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`]`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` visited `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`=`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` {start_url}`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` stack `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`=`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` [start_url]`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` while`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` stack:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` url `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`=`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` stack.pop()`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` for`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` link `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`in`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` html_parser.get_urls(url):`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` if`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` link `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`in`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` visited `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`or`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` link[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`7`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`:].split(`}),_jsx(_components.span,{style:{color:`#032F62`,\"--shiki-dark\":`#9ECBFF`},children:`\"/\"`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`)[`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:`0`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`] `}),_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:`!=`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` host:`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` continue`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` visited.add(link)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:` stack.append(link)`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#D73A49`,\"--shiki-dark\":`#F97583`},children:` return`}),_jsx(_components.span,{style:{color:`#005CC5`,\"--shiki-dark\":`#79B8FF`},children:` list`}),_jsx(_components.span,{style:{color:`#24292E`,\"--shiki-dark\":`#E1E4E8`},children:`(visited)`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`complexity`,children:`Complexity`}),`\n`,_jsxs(Table,{children:[_jsx(_components.thead,{children:_jsxs(_components.tr,{children:[_jsx(_components.th,{children:`Time`}),_jsx(_components.th,{children:`Space`})]})}),_jsx(_components.tbody,{children:_jsxs(_components.tr,{children:[_jsx(_components.td,{children:`O(V + E) pages and links visited once`}),_jsx(_components.td,{children:`O(V) visited set`})]})})]}),`\n`,_jsx(Heading,{level:`2`,id:`tags`,children:`Tags`}),`\n`,_jsxs(_components.p,{children:[_jsx(_components.a,{href:`/catalog/neetcode`,children:`NeetCode All`}),`.`]})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"codeStyling":{"theme":{"light":"github-light","dark":"github-dark"}},"config":{},"pageMetadata":{"title":"Web Crawler Python Solution with Tests","description":"Tested Python solution for LeetCode 1236 with 18 pytest cases. Generate a practice environment with lcpy.","href":"/problems/web-crawler"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Problem","slug":"problem","depth":2,"children":[{"title":"Examples","slug":"examples","depth":3,"children":[]},{"title":"Constraints","slug":"constraints","depth":3,"children":[]}]},{"title":"Solution","slug":"solution","depth":2,"children":[]},{"title":"Complexity","slug":"complexity","depth":2,"children":[]},{"title":"Tags","slug":"tags","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"leetcode-py","siteName":"leetcode-py","title":"Web Crawler Python Solution with Tests - leetcode-py","navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-09-07T03:04:53.335Z","gitLastModified":"2026-09-07T03:04:07.000Z","originalFileLocation":"problems/web-crawler.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Tested Python solution for LeetCode 1236 with 18 pytest cases. Generate a practice environment with lcpy.\">\u003cmeta data-mint-head name=\"application-name\" content=\"leetcode-py\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Web Crawler Python Solution with Tests - leetcode-py\">\u003cmeta data-mint-head property=\"og:description\" content=\"Tested Python solution for LeetCode 1236 with 18 pytest cases. Generate a practice environment with lcpy.\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"leetcode-py\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://leetcode-py.wisl.dev/problems/web-crawler\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://leetcode-py.wisl.dev/img/brand/og-card-light.png\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Web Crawler Python Solution with Tests - leetcode-py\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Tested Python solution for LeetCode 1236 with 18 pytest cases. Generate a practice environment with lcpy.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://leetcode-py.wisl.dev/img/brand/og-card-light.png\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/leetcode-py/MPF4VdYq1xxotsp3/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"leetcode-py\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#3776AB\">\u003clink data-mint-head rel=\"canonical\" href=\"https://leetcode-py.wisl.dev/problems/web-crawler\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/problems/web-crawler.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://leetcode-py.wisl.dev/#organization\",\"name\":\"leetcode-py\",\"url\":\"https://leetcode-py.wisl.dev\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/leetcode-py/aJKJ4BOUnXP-zM3G/img/brand/leetcode-py-lockup.svg?fit=max\u0026auto=format\u0026n=aJKJ4BOUnXP-zM3G\u0026q=85\u0026s=34c74a404ed256642a54345a92489c97\"},\"sameAs\":[\"https://github.com/wislertt/leetcode-py\",\"https://pypi.org/project/leetcode-py-sdk/\",\"https://wisl.dev\"]},{\"@type\":\"WebSite\",\"@id\":\"https://leetcode-py.wisl.dev#website\",\"name\":\"leetcode-py\",\"url\":\"https://leetcode-py.wisl.dev\",\"publisher\":{\"@id\":\"https://leetcode-py.wisl.dev/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://leetcode-py.wisl.dev/problems/web-crawler#webpage\",\"url\":\"https://leetcode-py.wisl.dev/problems/web-crawler\",\"name\":\"Web Crawler Python Solution with Tests\",\"description\":\"Tested Python solution for LeetCode 1236 with 18 pytest cases. Generate a practice environment with lcpy.\",\"dateModified\":\"2026-09-07T03:04:07.000Z\",\"isPartOf\":{\"@id\":\"https://leetcode-py.wisl.dev#website\"}},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://leetcode-py.wisl.dev/problems/web-crawler#article\",\"headline\":\"Web Crawler Python Solution with Tests\",\"name\":\"Web Crawler Python Solution with Tests\",\"description\":\"Tested Python solution for LeetCode 1236 with 18 pytest cases. Generate a practice environment with lcpy.\",\"url\":\"https://leetcode-py.wisl.dev/problems/web-crawler\",\"mainEntityOfPage\":{\"@id\":\"https://leetcode-py.wisl.dev/problems/web-crawler#webpage\"},\"image\":\"https://leetcode-py.wisl.dev/img/brand/og-card-light.png\",\"dateModified\":\"2026-09-07T03:04:07.000Z\",\"publisher\":{\"@id\":\"https://leetcode-py.wisl.dev/#organization\"},\"isPartOf\":{\"@id\":\"https://leetcode-py.wisl.dev#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"leetcode-py\",\"creator\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","siteName":"leetcode-py","nav":{"global":null,"groups":[{"group":"Welcome","pages":[{"title":"LeetCode Practice Environments in Python","sidebarTitle":"leetcode-py","href":"/index"}]},{"group":"Getting Started","pages":[{"title":"Install the lcpy CLI via pip or uv","sidebarTitle":"Installation","href":"/getting-started/installation"},{"title":"lcpy Quickstart: First Problem in 5 Commands","sidebarTitle":"Quickstart","href":"/getting-started/quickstart"},{"title":"LeetCode Practice Environment vs a Bare Editor","sidebarTitle":"Why leetcode-py","href":"/getting-started/why-leetcode-py"}]},{"group":"Practice","pages":[{"title":"Anatomy of a problem","href":"/practice/problem-anatomy"},{"title":"Test LeetCode Solutions Locally with pytest","sidebarTitle":"Test locally","href":"/practice/test-locally"},{"title":"LeetCode Python Test Cases with pytest","sidebarTitle":"Testing","href":"/practice/testing"},{"title":"Visualizations","href":"/practice/visualizations"},{"title":"Notebooks","href":"/practice/notebooks"}]},{"group":"CLI","pages":[{"title":"lcpy CLI: Generate and List Problems","sidebarTitle":"lcpy","href":"/cli/lcpy"},{"title":"Blind 75, Grind 75, NeetCode and Others","sidebarTitle":"Collections","href":"/cli/collections"}]},{"group":"Catalog","pages":[{"title":"LeetCode Problem Catalog: All Collections","sidebarTitle":"Catalog","href":"/catalog/index"},{"title":"All LeetCode Problems in Python with Tests","sidebarTitle":"All Problems","href":"/catalog/all"},{"title":"Easy LeetCode Problems in Python with Tests","sidebarTitle":"Easy","href":"/catalog/easy"},{"title":"Medium LeetCode Problems in Python with Tests","sidebarTitle":"Medium","href":"/catalog/medium"},{"title":"Hard LeetCode Problems in Python with Tests","sidebarTitle":"Hard","href":"/catalog/hard"},{"title":"Topics","href":"/catalog/topics"},{"title":"Grind 75 in Python with Tested Solutions","sidebarTitle":"Grind 75","href":"/catalog/grind-75"},{"title":"Grind Collection in Python: Tested Solutions","sidebarTitle":"Grind","href":"/catalog/grind"},{"title":"Blind 75 in Python with Tested Solutions","sidebarTitle":"Blind 75","href":"/catalog/blind-75"},{"title":"NeetCode 150 in Python with Tested Solutions","sidebarTitle":"NeetCode 150","href":"/catalog/neetcode-150"},{"title":"NeetCode 250 in Python with Tested Solutions","sidebarTitle":"NeetCode 250","href":"/catalog/neetcode-250"},{"title":"NeetCode All in Python with Tested Solutions","sidebarTitle":"NeetCode All","href":"/catalog/neetcode"},{"title":"AlgoMaster 75 in Python with Tested Solutions","sidebarTitle":"AlgoMaster 75","href":"/catalog/algo-master-75"}]},{"group":"Contributing","pages":[{"title":"Add a New LeetCode Problem End to End","sidebarTitle":"Problem creation","href":"/contributing/problem-creation"},{"title":"Contributor Reference: bakefile Tasks","sidebarTitle":"bakefile","href":"/contributing/bakefile"}]},{"group":"Troubleshooting","pages":[{"title":"lcpy Errors and Troubleshooting","sidebarTitle":"Troubleshooting","href":"/troubleshooting/index"},{"title":"lcpy gen Error: Problem Not Found","sidebarTitle":"Problem not found","href":"/troubleshooting/unknown-problem"},{"title":"lcpy gen Error: Problem Already Exists","sidebarTitle":"Already exists","href":"/troubleshooting/files-already-exist"},{"title":"Visualizations Show ASCII Instead of Diagrams","sidebarTitle":"ASCII diagrams","href":"/troubleshooting/graphviz-missing"},{"title":"Playground Notebook Cells Do Not Render","sidebarTitle":"Notebook cells","href":"/troubleshooting/notebook-cells"}]}]},"basePath":"","title":"Web Crawler Python Solution with Tests - leetcode-py","buildId":"dpl_42LwxxU1kGN114H4XzwGYXa9ZDah","deploymentId":"dpl_42LwxxU1kGN114H4XzwGYXa9ZDah","dynamics":["a74be98ba7"],"chunks":["zoom-image-lightbox-DkE6VzSN.js","lightbox-handle-r17bVUz3.js"]}