Spell-Check Your Site Using Pytest
Use Automation to Check Spelling
Here's a clever way to catch embarrassing spelling mistakes on your website using pytest
and spellchecker
. This script can be scheduled to run periodically to ensure nothing slips through the cracks!
Why Check for Spelling?
Spelling errors can reduce trust, affect SEO, and just look unprofessional. With this script, you can automatically scan your site's content and get alerted if anything seems off.
Dependencies
pytest
requests
beautifulsoup4
pyspellchecker
Install them with:
pip install pytest requests beautifulsoup4 pyspellchecker
The Test Code
Here is the full test you can drop into your test suite:
import pytest
import requests
from bs4 import BeautifulSoup
from spellchecker import SpellChecker
from urllib.parse import urljoin
def get_visible_text(url):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
except requests.RequestException as e:
pytest.fail(f"Failed to fetch {url}: {e}")
soup = BeautifulSoup(response.text, 'html.parser')
for element in soup(['script', 'style', 'header', 'footer', 'nav', 'aside']):
element.decompose()
text = soup.get_text(separator=' ', strip=True)
return text
def check_spelling(text, custom_words=None):
spell = SpellChecker()
if custom_words:
spell.word_frequency.load_words(custom_words)
words = text.split()
misspelled = spell.unknown(words)
return misspelled
def test_spelling_cryan_com():
url = "https://www.cryan.com"
custom_words = ["cryan", "blog", "tech", "xai"]
text = get_visible_text(url)
misspelled_words = check_spelling(text, custom_words=custom_words)
assert not misspelled_words, (
f"Spelling errors found on {url}: {misspelled_words}"
)
if __name__ == "__main__":
pytest.main(["-v", __file__])
Customization Tips
- Add more custom words to avoid false positives like brand names or domain-specific terms.
- Expand to multiple pages by looping through URLs and running the same logic.
- Integrate with CI/CD for automatic detection during deployment.