使用Python爬虫代码时遇AttributeError: 'NoneType'无find_all属性错误
Hey there! That error is one of the most common pitfalls when web scraping—let’s break down exactly what’s happening and walk through how to fix it.
What’s causing this error?
This message means the object you’re trying to call find_all() on is None. In most cases, this happens because you used a method like soup.find() or soup.select_one() to locate a parent element, but that method couldn’t find anything matching your selector, so it returned None. Then when you try to run None.find_all(), Python throws this error.
Step-by-step fixes & troubleshooting:
Verify your selector matches the actual page HTML
Chances are, your selector (like a class name, tag, or ID) is incorrect, or the website’s page structure has changed since you wrote the code.- Quick check: Print the object before calling
find_all()—for example, if you haveparent_div = soup.find('div', class_='product-container'), runprint(parent_div)first. If it outputsNone, fire up your browser’s dev tools (F12) to inspect the real HTML structure of the page. Double-check that the class name, tag type, or other attributes you’re using match exactly (note that class names are case-sensitive!).
- Quick check: Print the object before calling
Handle dynamic content if the site uses JavaScript
If you’re usingrequeststo fetch the page, you’re only getting the raw HTML sent by the server—any content loaded dynamically with JavaScript won’t be there. So if your target elements are rendered after the initial page load, yoursoup.find()call will returnNone.- Solutions: Either use tools like
SeleniumorPlaywrightto simulate a browser (which waits for JS to render content), or inspect the site’s network requests (via dev tools) to find the API endpoint that serves the dynamic data directly—this is usually faster and more reliable than scraping rendered pages.
- Solutions: Either use tools like
Add safety checks to your code
Even with perfect selectors, websites can change or block your requests unexpectedly. Add a simple conditional check to avoid crashes:# Example with safety check parent_element = soup.find('div', class_='target-section') if parent_element: child_items = parent_element.find_all('a', class_='product-link') # Process your child items here else: print("Warning: Could not locate the parent element on the page.")This way, your script will gracefully handle missing elements instead of throwing an error.
Confirm your request is successful
Sometimes your request might be blocked by anti-scraping measures, or you might get a server error (like 403 or 500). In these cases, the HTML you get back won’t have the elements you’re looking for.- Check the response status code:
print(response.status_code)after making your request. If it’s not 200, you’ll need to adjust your request—add a user-agent header, use a proxy, or include cookies to mimic a real browser. You can also printresponse.textto see if you’re getting a valid HTML page or an error message.
- Check the response status code:
内容的提问来源于stack exchange,提问作者Rupesh Hasthi

