Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/The best way to fix the attributeerror: 'nonetype' object has no attribute 'text' error
Next
Answered
Braxton Miller
  • 10
Braxton Miller
Asked: May 18, 20222022-05-18T20:52:24+00:00 2022-05-18T20:52:24+00:00In: python

The best way to fix the attributeerror: ‘nonetype’ object has no attribute ‘text’ error

  • 10

. Advertisement .

..3..

. Advertisement .

..4..

For the problem “attributeerror: ‘nonetype’ object has no attribute ‘text’.” I tried to fix it, but It doesn’t work and returns the result I want. Here is my program:

url = 'http://legis.senado.leg.br/dadosabertos/senador/4988/autorias'
 import requests
 from xml.etree import ElementTree
 
 response = requests.get(url, stream=True)
 response.raw.decode_content = True
 
 tree = ElementTree.parse(response.raw)
 
 root = tree.getroot()
 
 for child in root.iter('Materia'):
  if child.find('EmentaMateria').text is not None:
  ementa = child.find('EmentaMateria').text
 
  for child_IdMateria in child.findall('IdentificacaoMateria'):
  anoMateria = child_IdMateria.find('AnoMateria').text
  materia = child_IdMateria.find('NumeroMateria').text
  siglaMateria = child_IdMateria.find('SiglaSubtipoMateria').text
 
 
 
  print('Ano = '+anoMateria+' | Numero Materia = '+materia+' | tipo = '+siglaMateria+' | '+ementa)

and

---------------------------------------------------------------------------
 AttributeError Traceback (most recent call last)
 <ipython-input-70-77e5e1b79ccc> in <module>()
  11 
  12 for child in root.iter('Materia'):
 ---> 13 if not child.find('EmentaMateria').text is None:
  14 ementa = child.find('EmentaMateria').text
  15 
 
 AttributeError: 'NoneType' object has no attribute 'text'

has occurred. I’ve checked the entire command line, but still can’t find the mistake.

nonetype object has no attribute text
  • 2 2 Answers
  • 612 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-29T12:21:30+00:00Added an answer on June 29, 2022 at 12:21 pm

    The cause: A call to an attribute out of an object which does not present or is not supported results in an exception known as an attribute error in the Python program.

    Solution: You should follow my way to fix attributeerror: ‘nonetype’ object has no attribute ‘text’:

    You should verify that child.find('EmentaMateria') is not None before determining if child.find('EmentaMateria').text is not None.

    Additionally, in order to prevent calling child.find('EmentaMateria') twice, you need also keep the returning value.

    Finally, whether child.find('EmentaMateria') returns None, you need to assign ementa to a default value because otherwise the following print function will reference an uninitialized variable.

    Alter:

    if child.find('EmentaMateria').text is not None:
    ementa = child.find('EmentaMateria').text

    with:

    node = child.find('EmentaMateria')
    if node is not None:
    ementa = node.text
    else:
    ementa = None

    As an alternative, you can accomplish the same thing without a temporary variable by using the built-in function getattr:

    ementa = getattr(child.find('EmentaMateria'), 'text', None)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Maël Weber
    2022-05-25T21:10:38+00:00Added an answer on May 25, 2022 at 9:10 pm

    You can inspect the tags if you’re using the code to read an xml format file. In my case, there were some rogue tag at the end. I was able to remove them and the code worked as expected.

    • 14
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.