Nonetype object has no attribute word

I am getting an error message that says

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

How can I understand this message?

What general scenarios might cause such an AttributeError, and how can I identify the problem?


This is a special case of AttributeErrors. It merits separate treatment because there are a lot of ways to get an unexpected None value from the code, so it’s typically a different problem; for other AttributeErrors, the problem might just as easily be the attribute name.

See also What is a None value? and What is a ‘NoneType’ object? for an understanding of None and its type, NoneType.

Karl Knechtel's user avatar

asked Jan 20, 2012 at 23:38

Jacob Griffin's user avatar

Jacob GriffinJacob Griffin

4,6473 gold badges16 silver badges11 bronze badges

1

NoneType means that instead of an instance of whatever Class or Object you think you’re working with, you’ve actually got None. That usually means that an assignment or function call up above failed or returned an unexpected result.

answered Jan 20, 2012 at 23:40

g.d.d.c's user avatar

2

You have a variable that is equal to None and you’re attempting to access an attribute of it called ‘something’.

foo = None
foo.something = 1

or

foo = None
print(foo.something)

Both will yield an AttributeError: 'NoneType'

Błażej Michalik's user avatar

answered Jan 20, 2012 at 23:40

koblas's user avatar

koblaskoblas

24.9k6 gold badges38 silver badges48 bronze badges

2

The NoneType is the type of the value None. In this case, the variable lifetime has a value of None.

A common way to have this happen is to call a function missing a return.

There are an infinite number of other ways to set a variable to None, however.

answered Jan 20, 2012 at 23:41

S.Lott's user avatar

S.LottS.Lott

382k79 gold badges505 silver badges777 bronze badges

1

Consider the code below.

def return_something(someint):
 if  someint > 5:
    return someint

y = return_something(2)
y.real()

This is going to give you the error

AttributeError: ‘NoneType’ object has no attribute ‘real’

So points are as below.

  1. In the code, a function or class method is not returning anything or returning the None
  2. Then you try to access an attribute of that returned object(which is None), causing the error message.

answered Apr 10, 2017 at 5:32

PHINCY L PIOUS's user avatar

PHINCY L PIOUSPHINCY L PIOUS

3351 gold badge3 silver badges7 bronze badges

if val is not None:
    print(val)
else:
    # no need for else: really if it doesn't contain anything useful
    pass

Check whether particular data is not empty or null.

tripleee's user avatar

tripleee

172k32 gold badges264 silver badges311 bronze badges

answered Dec 21, 2020 at 12:52

Shah Vipul's user avatar

Shah VipulShah Vipul

5576 silver badges11 bronze badges

1

It means the object you are trying to access None. None is a Null variable in python.
This type of error is occure de to your code is something like this.

x1 = None
print(x1.something)

#or

x1 = None
x1.someother = "Hellow world"

#or
x1 = None
x1.some_func()

# you can avoid some of these error by adding this kind of check
if(x1 is not None):
    ... Do something here
else:
    print("X1 variable is Null or None")

answered Mar 3, 2019 at 9:08

M. Hamza Rajput's user avatar

M. Hamza RajputM. Hamza Rajput

7,2521 gold badge40 silver badges36 bronze badges

When building a estimator (sklearn), if you forget to return self in the fit function, you get the same error.

class ImputeLags(BaseEstimator, TransformerMixin):
    def __init__(self, columns):
        self.columns = columns

    def fit(self, x, y=None):
        """ do something """

    def transfrom(self, x):
        return x

AttributeError: ‘NoneType’ object has no attribute ‘transform’?

Adding return self to the fit function fixes the error.

answered Jan 29, 2020 at 15:56

Chiel's user avatar

ChielChiel

1,7561 gold badge10 silver badges24 bronze badges

1

g.d.d.c. is right, but adding a very frequent example:

You might call this function in a recursive form. In that case, you might end up at null pointer or NoneType. In that case, you can get this error. So before accessing an attribute of that parameter check if it’s not NoneType.

Prashant Sengar's user avatar

answered Feb 19, 2019 at 4:42

barribow's user avatar

barribowbarribow

993 silver badges4 bronze badges

2

You can get this error with you have commented out HTML in a Flask application. Here the value for qual.date_expiry is None:

   <!-- <td>{{ qual.date_expiry.date() }}</td> -->

Delete the line or fix it up:

<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>

answered Oct 23, 2019 at 10:37

Jeremy Thompson's user avatar

Jeremy ThompsonJeremy Thompson

60.6k33 gold badges186 silver badges312 bronze badges

None of the other answers here gave me the correct solution. I had this scenario:

def my_method():
   if condition == 'whatever':
      ....
      return 'something'
   else:
      return None

answer = my_method()

if answer == None:
   print('Empty')
else:
   print('Not empty')

Which errored with:

File "/usr/local/lib/python3.9/site-packages/gitlab/base.py", line 105, in __eq__
if self.get_id() and other.get_id():
AttributeError: 'NoneType' object has no attribute 'get_id'

In this case you can’t test equality to None with ==. To fix it I changed it to use is instead:

if answer is None:
   print('Empty')
else:
   print('Not empty')

tripleee's user avatar

tripleee

172k32 gold badges264 silver badges311 bronze badges

answered Jul 22, 2022 at 13:39

David Newcomb's user avatar

David NewcombDavid Newcomb

10.5k3 gold badges48 silver badges60 bronze badges

1

blog banner for post titled: How to Solve Guide for Python AttributeError

We raise a Python AttributeError when we try to call or access an attribute of an object that does not exist for that object.

This tutorial will go through what an attribute is, what the AttributeError is in detail, and we will go through four examples to learn how to solve the error.

Table of contents

  • What is a Python AttributeError?
  • Example #1: Trying to Use append() on a String
    • Solution
  • Example #2: Trying to Access an Attribute of a Class that does not exist
    • Solution
  • Example #3: NoneType Object has no Attribute
    • Solution
  • Example #4: Handling Modules
    • Solution
  • Summary

What is a Python AttributeError?

An attribute of an object is a value or a function associated with that object. We can express calling a method of a class as referencing an attribute of a class.

Let’s look at an example of a Python class for the particle electron

class Electron:
    def __init__(self):
        self.charge = -1
        self.mass = 0.51
        self.spin = 1/2
 
    def positron(self):
        self.charge = +1
        return self.charge

We can think of an attribute in Python as a physical attribute of an object. In this example, the fundamental particle, the electron, has physical attributes of charge, mass, and spin. The Electron class has the attributes charge, mass, and spin.

An attribute can also be a function. The function positron() returns the charge of the electron’s anti-particle, the positron.

Data types can have attributes. For example, the built-in data type List has the append() method to append elements to an existing list. Therefore, List objects support the append() method. Let’s look at an example of appending to a list:

a_list = [2, 4, 6]

a_list.append(8)

print(a_list)

Attributes have to exist for a class object or a data type for you to reference it. If the attribute is not associated with a class object or data type, you will raise an AttributeError.

Example #1: Trying to Use append() on a String

Let’s look at an example scenario where we concatenate two strings by appending one string to another.

string1 = "research"

string2 = "scientist"

string1.append(string2)

Using append() is impossible because the string data type does not have the append() method. Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
1 string1.append(string2)

AttributeError: 'str' object has no attribute 'append'

Solution

To solve this problem, we need to define a third string. We can then concatenate the two strings using the + symbol and assign the result to the third string. We can concatenate a space between the two strings so that the words do not run together. Let’s look at how the revised code:

string1 = "research"

string2 = "scientist"

string3 = string1 + " " + string2

print(string3)
research scientist

Example #2: Trying to Access an Attribute of a Class that does not exist

Let’s look at an example scenario where we want to access an attribute of a class that does not exist. We can try to create an instance of the class Electron from earlier in the tutorial. Once we have the instance, we can try to use the function get_mass() to print the mass of the electron in MeV.

class Electron:

   def __init__(self):

       self.charge = -1

       self.mass = 0.51

       self.spin = 1/2
  
   def positron(self):

       self.charge = +1

       return self.charge

electron = Electron()

mass = electron.get_mass()

If we try to run the code, we get the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
1 mass = electron.get_mass()

AttributeError: 'Electron' object has no attribute 'get_mass'

The Electron class has no attribute called get_mass(). Therefore we raise an AttributeError.

Solution

To solve this, we can do two things. We can add the method to the class and use a try-except statement. First, let’s look at adding the method:

class Electron:
    def __init__(self):
        self.charge = -1
        self.mass = 0.51
        self.spin = 1/2

    def positron(self):
        self.charge = +1
        return self.charge

    def get_mass(self):
            return self.mass
electron = Electron()

mass = electron.get_mass()

print(f' The mass of the electron is {mass} MeV')

 The mass of the electron is 0.51 MeV

Secondly, let’s look at using try-except to catch the AttributeError. We can use try-except statements to catch any error, not just AttributeError. Suppose we want to use a method called get_charge() to get the charge of the electron object, but we are not sure whether the Electron class contains the get_charge() attribute. We can enclose the call to get_charge() in a try-except statement.

class Electron:

    def __init__(self):

        self.charge = -1

        self.mass = 0.51

        self.spin = 1/2

    def positron(self):

        self.charge = +1

        return self.charge

    def get_mass(self):

            return self.mass

electron = Electron()

try:

    charge = electron.get_charge()

except Exception as e:

    print(e)
'Electron' object has no attribute 'get_charge'

Using try-except statements aligns with professional development and makes your programs less prone to crashing.

Example #3: NoneType Object has no Attribute

NoneType means that whatever class or object you are trying to access is None. Therefore, whenever you try to do a function call or an assignment for that object, it will raise the AttributeError: ‘NoneType’ object has no attribute. Let’s look at an example scenario for a specific NoneType attribute error. We will write a program that uses regular expressions to search for an upper case “S” character at the beginning and print the word. We need to import the module re for regular expression matching.

import re

# Search for an upper case "S" character in the beginning of a word then print the word

string = "Research Scientist"

for i in string.split():

    x = re.match(r"bSw+", i)

    print(x.group())

Let’s run the code and see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
      1 for i in string.split():
      2     x = re.match(r"bSw+", i)
      3     print(x.group())
      4 

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

We raise the AttributeError because there is no match in the first iteration. Therefore x returns None. The attribute group() does not belong to NoneType objects.

Solution

To solve this error, we want to only call group() for the situation where there is a match to the regular expression. We can therefore use the try-except block to handle the AttributeError. We can use continue to skip when x returns None in the for loop. Let’s look at the revised code.

import re

# Search for an upper case "S" character in the beginning of a word then print the word

string = "Research Scientist"
for i in string.split():
    x = re.match(r"bSw+", i)
    try:
        print(x.group())
    except AttributeError:
        continue
Scientist

We can see that the code prints out Scientist, which is the word that has an upper case “S” character.

Example #4: Handling Modules

We can encounter an AttributeError while working with modules because we may call a function that does not exist for a module. Let’s look at an example of importing the math module and calling a function to perform a square root.

import math

number = 9

square_root_number = math.square_root(number)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
1 square_root_number = math.square_root(number)

AttributeError: module 'math' has no attribute 'square_root'

The module math does not contain the attribute square_root. Therefore we get an AttributeError.

Solution

To solve this error, you can use the help() function to get the module’s documentation, including the functions that belong to the module. We can use the help function on the math module to see which function corresponds to the square root.

import math

help(math)
  sqrt(x, /)

        Return the square root of x.

The function’s name to return the square root of a number is sqrt(). We can use this function in place of the incorrect function name.

square_root_number = math.sqrt(number)

print(square_root_number)
3.0

The code successfully returns the square root of 9. You can also use help() on classes defined in your program. Let’s look at the example of using help() on the Electron class.

help(Electron)

class Electron(builtins.object)
 |  Methods defined here:
 |  
 |  __init__(self)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  get_mass(self)
 |  
 |  positron(self)

The help() function returns the methods defined for the Electron class.

Summary

Congratulations on reading to the end of this tutorial. Attribute errors occur in Python when you try to reference an invalid attribute.

  • If the attribute you want for built-in data types does not exist, you should look for an attribute that does something similar. For example, there is no append() method for strings but you can use concatenation to combine strings.
  • For classes that are defined in your code, you can use help() to find out if an attribute exists before trying to reference it. If it does not exist you can add it to your class and then create a new instance of the class.
  • If you are not sure if a function or value does not exist or if a code block may return a NoneType object, you can wrap the code in a try-except statement. Using a try-except stops your program from crashing if you raise an AttributeError.

For further reading on AttributeError, you can go to the following article: How to Solve Python AttributeError: ‘list’ object has no attribute ‘split’

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Introduction

Problem: How to solve “AttributeError: ‘NoneType’ object has no attribute ‘something’ “?

An AttributeError is raised in Python when you attempt to call the attribute of an object whose type does not support the method. For example, attempting to utilize the append() method on a string returns an AttributeError as lists use the append() function and strings don’t support it. 

Example:

# A set of strings
names = {"John", "Rashi"}
names.extend("Chris")
print(names)

Output:

Traceback (most recent call last):
  File "C:UsersSHUBHAM SAYONPycharmProjectsFinxerErrorsAttributeError - None Type Object.py", line 3, in <module>
    names.extend("Chris")
AttributeError: 'set' object has no attribute 'extend'

Hence, if you attempt to reference a value or function not related to a class object or data type, it will raise an AttributeError.

AttributeError:’NoneType’ object has no attribute ‘something’

Different reasons raise AttributeError: 'NoneType' object has no attribute 'something'. One of the reasons is that NoneType implies that instead of an instance of whatever Class or Object that you are working with, in reality, you have got None. It implies that the function or the assignment call has failed or returned an unforeseen outcome. 

Let’s have a look at an example that leads to the occurrence of this error.

Example 1:

# Assigning value to the variable 
a = None
print(a.something)

Output:

Traceback (most recent call last):
  File "C:UsersSHUBHAM SAYONPycharmProjectsFinxerErrorsAttributeError - None Type Object.py", line 3, in <module>
    print(a.something)
AttributeError: 'NoneType' object has no attribute 'something'

Hence, AttributeError: ‘NoneType’ object has no attribute ‘something’ error occurs when the type of object you are referencing is None.  It can also occur when you reference a wrong function instead of the function used in the program.

Example:

# A function to print numbers
def fun(x):
    if x <= 10:
        x = x + 1
    return x


a = fun(5)
# Calling the function that does not exist
print(a.foo())

Output:

Traceback (most recent call last):
  File "C:UsersSHUBHAM SAYONPycharmProjectsFinxerErrorsAttributeError - None Type Object.py", line 10, in <module>
    print(a.foo())
AttributeError: 'int' object has no attribute 'foo'

How to check if the operator is Nonetype?

Since this AttributeError revolves around the NoneType object, hence it is of primary importance to identify if the object referred has a type None. Thus, you can check if the operator is Nonetype with the help of the “is” operator. It will return True if the object is of the NoneType and return False if not.

Example:

x = None
if x is None:
    print("The value is assigned to None")
else:
    print(x)

Output:

The value is assigned to None

Now that you know how AttributeError: ‘NoneType’ object has no attribute ‘something’ gets raised let’s look at the different methods to solve it.

#Fix 1: Using if and else statements

You can eliminate the AttributeError: 'NoneType' object has no attribute 'something' by using the- if and else statements. The idea here is to check if the object has been assigned a None value.  If it is None then just print a statement stating that the value is Nonetype which might hamper the execution of the program.

Example:

x1 = None
if x1 is not None:
    x1.some_attribute = "Finxter"
else:
    print("The type of x1 is ", type(x1))

Output:

The type of x1 is  <class 'NoneType'>

#Fix 2: Using try and except Blocks

You can also use the exception handling (try and except block) to solve the AttributeError: 'NoneType' object has no attribute 'something'.

Example:

# A function to print numbers
def fun(x):
    if x <= 10:
        x = x + 1
    return x


a = fun(5)
# Using exception handling (try and except block)
try:
    print(a)
    # Calling the function that does not exist
    print(a.foo())

except AttributeError as e:
    print("The value assigned to the object is Nonetype")

Output:

6
The value assigned to the object is Nonetype

Quick Review

Now that we have gone through the ways to fix this AttributeError, let’s go ahead and visualize a few other situations which lead to the occurrence of similar attribute errors and then solve them using the methods we learned above.

1. ‘NoneType’ Object Has No Attribute ‘Group’

import re
# Search for an upper case "S" character in the beginning of a word, and print the word:
txt = "The rain in Spain"
for i in txt.split():
    x = re.match(r"bSw+", i)
    print(x.group())

Output:

Traceback (most recent call last):
  File "D:/PycharmProjects/Errors/attribute_error.py", line 9, in <module>
    print(x.group())
AttributeError: 'NoneType' object has no attribute 'group'

The code encounters an attribute error because in the first iteration it cannot find a match, therefore x returns None. Hence, when we try to use the attribute for the NoneType object, it returns an attribute error.

Solution: Neglect group() for the situation where x returns None and thus does not match the Regex. Therefore use the try-except blocks such that the attribute error is handled by the except block. 

import re
txt = "The rain in Spain"
for i in txt.split():
    x = re.match(r"bSw+", i)
    try:
        print(x.group())
    except AttributeError:
        continue

# Output : Spain

2. Appending list But error ‘NoneType’ object has no attribute ‘append’

li = [1, 2, 3, 4]
for i in range(5):
    li = li.append(i)
print(li)

Output:

Traceback (most recent call last):
  File "C:UsersSHUBHAM SAYONPycharmProjectsFinxerErrorsAttributeError - None Type Object.py", line 3, in <module>
    li = li.append(i)
AttributeError: 'NoneType' object has no attribute 'append'

Solution:

When you are appending to the list as i = li.append(i) you are trying to do an inplace operation which modifies the object and returns nothing (i.e. None). In simple words, you should not assign the value to the li variable while appending, it updates automatically. This is how it should be done:

li = [1, 2, 3, 4]
for i in range(5):
    li.append(i)
print(li)

# output: [1, 2, 3, 4, 0, 1, 2, 3, 4]

Conclusion

To sum things up, there can be numerous cases wherein you wil encounter an attribute error of the above type. But the underlying reason behind every scenario is the same, i.e., the type of object being referenced is None. To handle this error, you can try to rectify the root of the problem by ensuring that the object being referenced is not None. You may also choose to bypass the error based on the requirement of your code with the help of try-cath blocks.

I hope this article helped you to gain a deep understanding of attribute errors. Please stay tuned and subscribe for more interesting articles and discussions.


To become a PyCharm master, check out our full course on the Finxter Computer Science Academy available for free for all Finxter Premium Members:

shubham finxter profile image

I am a professional Python Blogger and Content creator. I have published numerous articles and created courses over a period of time. Presently I am working as a full-time freelancer and I have experience in domains like Python, AWS, DevOps, and Networking.

You can contact me @:

UpWork
LinkedIn

Я получаю ошибку, которая говорит

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

Код, который у меня есть, слишком длинный, чтобы публиковать здесь. Какие общие сценарии могут вызвать эту AttributeError, что должен означать NoneType и как я могу сузить то, что происходит?

4b9b3361

Ответ 1

NoneType означает, что вместо экземпляра любого класса или объекта, с которым вы работаете, вы действительно получили None. Это обычно означает, что вызов вызова или функции выше не выполнен или возвращает неожиданный результат.

Ответ 2

У вас есть переменная, которая равна None, и вы пытаетесь получить доступ к атрибуту, который он назвал «чем-то».

foo = None
foo.something = 1

или

foo = None
print foo.something

Оба будут давать AttributeError: 'NoneType'

Ответ 3

Другие объяснили, что такое NoneType и общий способ его завершения (т.е. отказ вернуть значение из функции).

Еще одна распространенная причина, по которой у вас есть None, где вы не ожидаете, что это назначение операции на месте изменяемого объекта. Например:

mylist = mylist.sort()

Метод sort() списка сортирует список на месте, т.е. изменяется mylist. Но фактическое возвращаемое значение метода None, а не отсортированный список. Итак, вы только что назначили None на mylist. Если вы попытаетесь сделать следующее, скажем, mylist.append(1) Python даст вам эту ошибку.

Ответ 4

NoneType — тип значения None. В этом случае переменная lifetime имеет значение None.

Общим способом для этого является вызов функции без return.

Существует множество других способов установить переменную в None.

Ответ 5

Рассмотрим приведенный ниже код.

def return_something(someint):
 if  someint > 5:
    return someint

y = return_something(2)
y.real()

Это приведет к ошибке

AttributeError: объект «NoneType» не имеет атрибута «real»

Итак, точки такие, как показано ниже.

  • В коде функция или метод класса ничего не возвращает или возвращает None
  • Затем вы пытаетесь получить доступ к атрибуту этого возвращенного объекта (который является None), вызывая сообщение об ошибке.

Ответ 6

Это означает, что объект, к которому вы пытаетесь получить доступ, None. None является Null переменной в python. Этот тип ошибки происходит, потому что ваш код выглядит примерно так.

x1 = None
print(x1.something)

#or

x1 = None
x1.someother = "Hellow world"

#or
x1 = None
x1.some_func()

# you can avoid some of these error by adding this kind of check
if(x1 is not None):
    ... Do something here
else:
    print("X1 variable is Null or None")

Ответ 7

gddc прав, но добавляя очень частый пример:

Вы можете вызвать эту функцию в рекурсивной форме. В этом случае вы можете получить нулевой указатель или NoneType. В этом случае вы можете получить эту ошибку. Поэтому, прежде чем получить доступ к атрибуту этого параметра, проверьте, не NoneType ли он NoneType.

Answer by Alana Serrano

A big gotcha when parsing websites is that the source code can look very different in your browser when compared to what requests sees. The difference is javascript, which can hugely modify the DOM in a javascript capable browser. ,

I can’t see that id by entering your link in browser. What is the expected text to be returned?

– QHarr

Dec 30 ’18 at 18:45

,Please be sure to answer the question. Provide details and share your research!,use requests to get the page, and then examine it closely — does that tag exist when the page is retrieved by a non-js enabled agent?

Next time use the query string exactly as it is.

import requests
from bs4 import BeautifulSoup
search="2%2B2"
link="https://www.google.com/search?q="+search
headers={'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}
source=requests.get(link,headers=headers).text
soup=BeautifulSoup(source,"html.parser")
answer=soup.find('span',id="cwos")
print(answer.text)

Output:

 4  

Answer by Donald Blackwell

NoneType object has no attribute text ,97123/nonetype-object-has-no-attribute-text,As per the Beautiful Soup Documentation:,How to prompt for user input and read command-line arguments? Jan 5

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
driver = webdriver.Chrome("D:/chromedriver")
products=[] #List to store name of the product
prices=[] #List to store price of the product
Descriptions=[] #List to store rating of the product
driver.get("https://www.nike.com/w/womens-lifestyle-shoes-13jrmz5e1x6zy7ok")
content = driver.page_source
soup = BeautifulSoup(content)
for a in soup.find_all('a',href=True, attrs={'class':'product-card__link-overlay'}):
    name = a.find('div', attrs={'class':'product-card__title'})
    price=a.find('div', attrs={'class':'product-card__price'})
    products.append(name.text)
    prices.append(price.text)
    Descriptions=a.find('li', attrs={'class':'product-card__subtitle'}) 
    #ratings.append(rating.text) 
df = pd.DataFrame({'Product Name':products,'Price':prices, 'Description' :Descriptions}) 
#,'Rating':ratings
df.to_csv('scraping .csv', index=False, encoding='utf-8')
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
driver = webdriver.Chrome("D:/chromedriver")
products=[] #List to store name of the product
prices=[] #List to store price of the product
Descriptions=[] #List to store rating of the product
driver.get("https://www.nike.com/w/womens-lifestyle-shoes-13jrmz5e1x6zy7ok")
content = driver.page_source
soup = BeautifulSoup(content)
for a in soup.find_all('a',href=True, attrs={'class':'product-card__link-overlay'}):
    name = a.find('div', attrs={'class':'product-card__title'})
    price=a.find('div', attrs={'class':'product-card__price'})
    products.append(name.text)
    prices.append(price.text)
    Descriptions=a.find('li', attrs={'class':'product-card__subtitle'}) 
    #ratings.append(rating.text) 
df = pd.DataFrame({'Product Name':products,'Price':prices, 'Description' :Descriptions}) 
#,'Rating':ratings
df.to_csv('scraping .csv', index=False, encoding='utf-8')


AttributeError                            Traceback (most recent call last)
<ipython-input-38-5c26eac7db21> in <module>
      6     price=a.find('div', attrs={'class':'product-card__price'})
      7     #rating=a.find('div', attrs={'class':'_3LWZlK'})
----> 8     products.append(name.text)
      9     prices.append(price.text)
     10     Decsriptions=a.find('li', attrs={'class':'product-card__subtitle'})

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

Answer by Everleigh Jimenez

I am trying to find and extract the genre from the IMDb page and I get this error (AttributeError: ‘NoneType’ object has no attribute ‘text’) when running my code below. I can’t figure out why I am unable to find it, as the genre is text.,Thanks now I am just trying to bring it all together. The issue now being that I don’t get all the results, only a single page.,It doesn’t look like there’s anything within an h3 element that is a span of class “genre.”

I am trying to find and extract the genre from the IMDb page and I get this error (AttributeError: ‘NoneType’ object has no attribute ‘text’) when running my code below. I can’t figure out why I am unable to find it, as the genre is text.

genre = container.h3.find('span', class_ = 'genre').text
genres.append(genre)

https://www.imdb.com/search/title?title_type=feature&release_date=

</h3>
    <p class="text-muted ">
            <span class="certificate">R</span>
                 <span class="ghost">|</span> 
                <span class="runtime">154 min</span>
                 <span class="ghost">|</span> 
            <span class="genre">
Crime, Drama            </span>
    </p>
    <div class="ratings-bar">
    <div class="inline-block ratings-imdb-rating" name="ir" data-value="8.9">
        <span class="global-sprite rating-star imdb-rating"></span>
        <strong>8.9</strong>
    </div>
            <div class="inline-block ratings-user-rating">
                <span class="userRatingValue" id="urv_tt0110912" data-tconst="tt0110912">
                    <span class="global-sprite rating-star no-rating"></span>
                    <span name="ur" data-value="0" class="rate" data-no-rating="Rate this">Rate this</span>
                </span>

Answer by Hadleigh Camacho

TypeError: message.client.commands.array is not a function
,
javascript slice array of objects by date
,
What is the meaning of “Object [RegExp String Iterator] {}” in NodeJs
,I tried it and it worked fine after that.

import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
import re
import sys, getopt
import csv

def get_tables(url):
    res = requests.get(url)
    ## The next two lines get around the issue with comments breaking the parsing.
    comm = re.compile("<!--|-->")
    soup = BeautifulSoup(comm.sub("",res.text),'lxml')
    all_tables = soup.findAll("tbody")
    team_table = all_tables[0]
    player_table = all_tables[1]
    return player_table, team_table

def get_frame(features, player_table):
    pre_df_player = dict()
    features_wanted_player = features
    rows_player = player_table.find_all('tr')
    for row in rows_player:
        if(row.find('th',{"scope":"row"}) != None):
    
            for f in features_wanted_player:
                cell = row.find("td",{"data-stat": f})
                a = cell.text.strip().encode()
                text=a.decode("utf-8")
                if(text == ''):
                    text = '0'
                if((f!='player')&(f!='nationality')&(f!='position')&(f!='squad')&(f!='age')&(f!='birth_year')):
                    text = float(text.replace(',',''))
                if f in pre_df_player:
                    pre_df_player[f].append(text)
                else:
                    pre_df_player[f] = [text]
    df_player = pd.DataFrame.from_dict(pre_df_player)
    return df_player

stats = ["player","nationality","position","squad","age","birth_year","games","games_starts","minutes","goals","assists","pens_made","pens_att","cards_yellow","cards_red","goals_per90","assists_per90","goals_assists_per90","goals_pens_per90","goals_assists_pens_per90","xg","npxg","xa","xg_per90","xa_per90","xg_xa_per90","npxg_per90","npxg_xa_per90"]

def frame_for_category(category,top,end,features):
    url = (top + category + end)
    player_table, team_table = get_tables(url)
    df_player = get_frame(features, player_table)
    return df_player

top='https://fbref.com/en/comps/9/'
end='/Premier-League-Stats'
df1 = frame_for_category('stats',top,end,stats)

df1

Answer by Damian Camacho

Web Scraping & Web Development

wo_span_name = w_span_name.text
AttributeError: 'NoneType' object has no attribute 'text'

"<div class=""phones phone primary"">(Data I am looking for)</div>"

##Already have code to pull, and parcer the site into html
for findcard in soupurl.find_all('div', class_="v-card"):
    #Declaring global varibles
    global wo_span_name
    global wo_div_phone
    #grab name
    w_span_name = findcard.find('a', class_='business-name')
    wo_span_name = w_span_name.text
    #grab phone number

    w_div_phone = findcard.find('div', class_="phones phone primary")
    wo_div_phone = w_div_phone.text
    #Adding items to list
    phone_list.append(wo_div_phone)
    name_list.append(wo_span_name)

Answer by Carmelo Warren

I was web-scraping weather-searched Google with bs4, and Python can’t find a <span> tag when there is one. How can I solve this problem?,I tried to find this <span> with the class and the id, but both failed. ,Above is the HTML code I was trying to scrape in the page:,But failed with this code, the error is:

I tried to find this <span> with the class and the id, but both failed.

<div id="wob_dcp">
    <span class="vk_gy vk_sh" id="wob_dc">Clear with periodic clouds</span>    
</div>

Above is the HTML code I was trying to scrape in the page:

response = requests.get('https://www.google.com/search?hl=ja&ei=coGHXPWEIouUr7wPo9ixoAg&q=%EC%9D%BC%EB%B3%B8+%E6%A1%9C%E5%B7%9D%E5%B8%82%E7%9C%9F%E5%A3%81%E7%94%BA%E5%8F%A4%E5%9F%8E+%EB%82%B4%EC%9D%BC+%EB%82%A0%EC%94%A8&oq=%EC%9D%BC%EB%B3%B8+%E6%A1%9C%E5%B7%9D%E5%B8%82%E7%9C%9F%E5%A3%81%E7%94%BA%E5%8F%A4%E5%9F%8E+%EB%82%B4%EC%9D%BC+%EB%82%A0%EC%94%A8&gs_l=psy-ab.3...232674.234409..234575...0.0..0.251.929.0j6j1......0....1..gws-wiz.......35i39.yu0YE6lnCms')
soup = BeautifulSoup(response.content, 'html.parser')

tomorrow_weather = soup.find('span', {'id': 'wob_dc'}).text

But failed with this code, the error is:

Traceback (most recent call last):
  File "C:Userssungn_000Desktopweather.py", line 23, in <module>
    tomorrow_weather = soup.find('span', {'id': 'wob_dc'}).text
AttributeError: 'NoneType' object has no attribute 'text'

Answer by Sofia Willis

super().__init__(*args, **kwargs)

AttributeError: 'NoneType' Object Has No Attribute 'Text' in Python

This error happens when you try to call a method from an object that is None or not initiated. Before calling a method, you need to check if the object is None or not to eliminate this error; after that, call the desired method.

AttributeError is an exception you get while you call the attribute, but it is not supported or present in the class definition.

Causes and Solutions of the AttributeError: 'NoneType' object has no attribute 'text' in Python

This error is common while you’re doing web scraping or XML parsing. During the parsing, you’ll get this error if you get unstructured data.

Here are some more reasons:

  1. Data that JavaScript dynamically rendered.
  2. Scraping multiple pages with the same data.
  3. While parsing XML, if the node you are searching is not present.

Here are the common solutions you can try to get rid of the error:

  1. Check if the element exists before calling any attribute of it.
  2. Check the response to the request.

an Example Code

As this error often comes from web scraping, let’s see an example of web scraping. We will try to get the title of the StackOverflow website with our Python script.

Here is the code:

from bs4 import BeautifulSoup as bs
import requests
url = "https://www.stackoverflow.com/"

html_content = requests.get(url).text
soup = bs(html_content, "html.parser")

if soup.find('title').text is not None:
  print(soup.find('title').text)

Output:

Stack Overflow - Where Developers Learn, Share, & Build Careers

Here, you will notice that the if condition we used is is not None to ensure we’re calling an existing method. Now, it will not show any error such as the AttributeError.

При парсинге периодически выдается ошибка «AttributeError: ‘NoneType’ object has no attribute ‘text'», т.к. парсер не может найти нужный элемент по селектору и узнать у него text.

Данные получаю так:
fax = soup.find(id="ctl0_left_fax")

Затем добавляю в массив (на этом скрипт спотыкается): 'fax': fax.text.strip(),

Пробовал сделать проверку:

#  Проверяю, если у страницы TITLE пустой, значит там нечего парсить. ХОЧУ ПРОПУСТИТЬ ДАЛЬНЕЙШИЙ  ПАРСИНГ
if (len(soup.title.text.strip()) == 15) or (soup.title.text.strip() == 'testtesttest -'):
    exit
#  Если у страницы TITLE <16 (значит там есть какой-то контент), то посмотреть текст у селекторов
else:
    list = {
               'cap': cap.text.strip(),
               'fax': fax.text.strip(),
               'email': email.text.strip(),
        }

Но почему-то это игнорируется (хотя проверял — работает).

Добавлю, что реализовал подобный алгоритм на PHP и у меня все работало.

Более того, на этапе добавления в массив я ПРЯМО В НЕМ проверял через возвращаемый тип данных через тернарный оператор, но в питоне это не проходит.

Понравилась статья? Поделить с друзьями:
  • Nonetheless is it one word
  • Nonce word occasionalism is
  • Non word что такое
  • Non word character что это
  • Non verbal communication word