AttributeError Traceback (most recent call last) <ipython-input-3-3a9954822f37> in <module> 16 for book in soup.find_all('div', class_='book'): 17 # 获取书籍名称 ---> 18 book_name = book.find('h2', class_='book-name').text.strip() 19 20 # 创建以书名命名的文件夹 AttributeError: 'NoneType' object has no attribute 'text'报错如何解决
时间: 2023-12-06 17:45:33 浏览: 26
这个错误是因为在执行 `book.find('h2', class_='book-name')` 操作时,返回了 `None`,而 `None` 对象没有 `text` 属性,所以会抛出 `AttributeError` 异常。
你可以先检查 `book.find('h2', class_='book-name')` 是否返回了 `None` 对象,如果是的话,就跳过这个书籍,或者输出一个错误信息。
例如,你可以这样修改代码:
```python
for book in soup.find_all('div', class_='book'):
# 获取书籍名称
book_name_element = book.find('h2', class_='book-name')
if book_name_element is None:
print('无法获取书籍名称,跳过该书籍')
continue
book_name = book_name_element.text.strip()
# 创建以书名命名的文件夹
...
```
这样就可以避免抛出 `AttributeError` 异常了。
相关问题
AttributeError Traceback (most recent call last) <ipython-input-18-484e38e099dc> in <module>() 1 from sklearn import preprocessing ----> 2 minmiax_scale = preprocessing.MinMaxSacler((0,1)) AttributeError: module 'sklearn.preprocessing' has no attribute 'MinMaxSacler'
您好!根据错误信息,看起来是由于您拼写错误导致的。正确的类名应该是 `MinMaxScaler` 而不是 `MinMaxSacler`。请修改代码如下:
```python
from sklearn import preprocessing
minmax_scale = preprocessing.MinMaxScaler((0,1))
```
请注意修改后的代码中的 `MinMaxScaler` 拼写正确。如果还有其他问题,请随时提问。
result = X_train.join(y_train) result.head()AttributeError Traceback (most recent call last) <ipython-input-32-957a4f0008b1> in <module> ----> 1 result = X_train.join(y_train) 2 result.head() AttributeError: 'numpy.ndarray' object has no attribute 'join'
这个错误是因为 numpy.ndarray 对象没有 join() 方法。如果你想将 X_train 和 y_train 拼接在一起,可以使用 numpy.concatenate() 方法。例如:result = numpy.concatenate((X_train, y_train), axis=1)。然后,你可以使用 result.head() 方法查看结果的前几行。