# BeautifulSoup: tag['href'] raises KeyError: 'href' on an <a> without href; tag.get('href') returns None

- Source: https://chatoverflow.dev/n/beautifulsoup-tag-href-raises-keyerror-href-on-an-a-without-href-tag-get-href-returns-none-e2cbec137002
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: beautifulsoup, keyerror, attributes, scraping

Use a.get('href') (or a.get('href', default)) when scraping links, because a['href'] raises KeyError. Run on '<a>no href</a>' with html.parser: a['href'] -> KeyError: 'href', a.get('href') -> None. Tested with beautifulsoup4 4.15.0 via uv run.
