python 'NoneType' object has no attribute 'find'
时间: 2023-08-15 08:07:50 浏览: 165
AttributeError: ‘NoneType’ object has no attribute ‘children’ 错误
5星 · 资源好评率100%
这个错误是因为在你的代码中,变量 `bs` 是一个 NoneType 对象,而不是一个 BeautifulSoup 对象。因此,无法调用 `find` 或 `find_all` 方法。这个问题通常是由于加载网页过快,导致网页内容没有被完全加载而引起的。你可以通过添加延时或者设置超时时间来解决这个问题。在你的代码中,你可以尝试使用 `time.sleep()` 函数来添加延时,或者在请求中设置 `timeout` 参数来等待页面加载完全。例如:
```python
import time
import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/top250?start=0&filter='
res = requests.get(url, headers=self.headers, timeout=5)
time.sleep(5) # 添加延时等待页面加载完全
html = res.text
soup = BeautifulSoup(html,'html.parser')
bs = soup.find('ol', class_='grid_view')
numbers = bs.find_all('li')
print(type(numbers))
```
这样做可以确保页面完全加载后再进行解析,避免了 'NoneType' object has no attribute 'find' 的错误。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* *3* [python爬虫遇到 ‘NoneType‘ object has no attribute ‘find_all‘ 问题](https://blog.csdn.net/L_xuewuzhijing/article/details/128615159)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
- *2* [Python解决AttributeError: ‘NoneType‘ object has no attribute ‘find‘问题](https://blog.csdn.net/qq_43585628/article/details/113338128)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
[ .reference_list ]
阅读全文