Extract All Links from HTML
Medium
Write a function extract_links(html) that takes a string of HTML and returns a list of all href attribute values found in <a> tags, using BeautifulSoup.
Input
A string containing HTML markup.
Output
A list of href strings.
Example
Input
'<a href="https://a.com">A</a><a href="https://b.com">B</a>'
Output
['https://a.com', 'https://b.com']
Use BeautifulSoup(html, "html.parser").find_all("a") to get all anchor tags, then extract link.get("href") for each.
15 XP on solve
Back to lesson
Your Solution
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to submit your solution and earn XP.