Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

77 lines
3.2 KiB
Markdown

---
id: 6a0dcd03ee4e68698080ef6a
title: "Challenge 306: HTML Content Extractor"
challengeType: 29
dashedName: challenge-306
---
# --description--
Given a string of HTML, return the plain text content with all tags removed.
# --hints--
`extract_content('<p>hello world</p>')` should return `"hello world"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_content('<p>hello world</p>'), "hello world")`)
}})
```
`extract_content('<p>hello <span>world</span></p>')` should return `"hello world"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_content('<p>hello <span>world</span></p>'), "hello world")`)
}})
```
`extract_content('<a href="example.com">Click me</a>')` should return `"Click me"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_content('<a href="example.com">Click me</a>'), "Click me")`)
}})
```
`extract_content('<p><button onClick="learnToCode()">Learn</button> to <code>code<code> <br/>for <strong>free</strong> <br/>on <a href="https://freecodecamp.org/" target="_blank"><span class="highlight">freecodecamp</span>.org</a>')` should return `"Learn to code for free on freecodecamp.org"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_content('<p><button onClick="learnToCode()">Learn</button> to <code>code<code> <br/>for <strong>free</strong> <br/>on <a href="https://freecodecamp.org/" target="_blank"><span class="highlight">freecodecamp</span>.org</a>'), "Learn to code for free on freecodecamp.org")`)
}})
```
`extract_content('<div class="container"><h1 id="title">Welcome to <strong>My</strong> Website.</h1><p>This is a <a href="https://example.com" target="_blank">link</a> to something <em>really</em> <span class="highlight">important</span>.</p><ul><li>Item <strong>one</strong></li><li>Item <em>two</em></li><li>Item three</li></ul><img src="pic.jpg" alt="A picture"/><p class="footer">Contact us at <a href="mailto:hello@example.com">hello@example.com</a> for <span>more <strong>info</strong></span>.</p></div>')` should return `"Welcome to My Website.This is a link to something really important.Item oneItem twoItem threeContact us at hello@example.com for more info."`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(extract_content('<div class="container"><h1 id="title">Welcome to <strong>My</strong> Website.</h1><p>This is a <a href="https://example.com" target="_blank">link</a> to something <em>really</em> <span class="highlight">important</span>.</p><ul><li>Item <strong>one</strong></li><li>Item <em>two</em></li><li>Item three</li></ul><img src="pic.jpg" alt="A picture"/><p class="footer">Contact us at <a href="mailto:hello@example.com">hello@example.com</a> for <span>more <strong>info</strong></span>.</p></div>'), "Welcome to My Website.This is a link to something really important.Item oneItem twoItem threeContact us at hello@example.com for more info.")`)
}})
```
# --seed--
## --seed-contents--
```py
def extract_content(html):
return html
```
# --solutions--
```py
import re
def extract_content(html):
return re.sub(r'<[^>]*>', '', html).strip()
```