Home › Python

AttributeError: 'NoneType' object has no attribute — how to fix

AttributeError: 'NoneType' object has no attribute 'group'

Quick fix: The object before the . is None. Check what produced it — a failed re.search(), a BeautifulSoup find() that matched nothing, or a function without return — and add an if x is None check.

What it means

In x.something, x is None. The attribute name tells you what you expected x to be.

Attribute in the error Usual source of None
'group' re.match() / re.search() found no match
'text', 'find', 'get' BeautifulSoup find() / select_one() matched nothing
'append' my_list = my_list.append(x) (append returns None)
'cursor', 'execute' A connection function returned None after failing
any A function that has no return statement

Fix: handle the None case

m = re.search(r"id=(\d+)", url)
if m:
    user_id = m.group(1)
else:
    user_id = None   # or raise, or skip
title = soup.find("h1")
text = title.get_text(strip=True) if title else ""

Web scraping: why find() returns None

Don't reassign in-place methods

items = items.append(4)   # items is now None
items.append(4)           # correct

Last updated 2026-10-27.