#65MediumFiles & Modules· Standard Library
Web Scraper: Extract Article Links
Input format
`html`: an HTML document as a string. `base_url`: the site's address, such as `"https://blog.example.com"`.
Output format
A list of `[title, url]` pairs.
Examples
Input: html = "<h2><a href=\"/posts/intro\">Getting started</a></h2><p><a href=\"/about\">About</a></p><h2><a href=\"https://other.example.org/x\">Elsewhere</a></h2>", base_url = "https://blog.example.com"
Output: [["Getting started","https://blog.example.com/posts/intro"],["Elsewhere","https://other.example.org/x"]]
The `About` link is not inside an `<h2>`. The relative link gets the base URL, and the double space in the title is collapsed.
Input: html = "<h2><a href=\"#top\">Top</a> <a>Draft</a></h2>", base_url = "https://blog.example.com/"
Output: []
A fragment link and a link without `href` are both skipped.
Constraints
- len(html) <= 10^5
- Headings are not nested inside each other
Study first
- #python
- #web-scraping
- #html
- #standard-library
