Home > Net >  This is The Error :AttributeError: 'NoneType' object has no attribute 'text'
This is The Error :AttributeError: 'NoneType' object has no attribute 'text'

Time:11-08

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9620/2324248033.py in <module>
      7     for num in range(start, finish):
      8         print(f"Checking {num}...")
----> 9         num_status = check_seatNumber(num)
     10         if not num_status: print(f'{num} is valid')
     11         else: print(f'{num} is invalid')

~\AppData\Local\Temp/ipykernel_9620/948570639.py in check_seatNumber(seatNumber)
      2     response = requestSeatNumber(seatNumber)
      3     soup = BeautifulSoup(response.content)
----> 4     sent = soup.find('p',attrs={'style':"font-size: 14px;color: red; margin-top: 11px;"}).text==''
      5     print(sent)
      6 

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



this error appear to me after long run to my code Help me to solve it

def check_seatNumber(seatNumber): response = requestSeatNumber(seatNumber) soup = BeautifulSoup(response.content) sent = soup.find('p',attrs={'style':"font-size: 14px;color: red; margin-top: 11px;"}).text=='' print(sent)

if (soup.find('p',attrs={'style':"font-size: 14px;color: red; margin-top: 11px;"}).text==''):
    #getStudentInfo()
    student_inform(soup)
else: return seatNumber

CodePudding user response:

soup.find() can return None, and the NoneType object doesn't have a text attribute. You can get around this by splitting up soup.find(...).text into something like:

found = soup.find('p',attrs={'style':"font-size: 14px;color: red; margin-top: 11px;"})
if found:
    text = found.text
    # Check text

Here, if found is None, then the if statement isn't entered and no error will be thrown.

  • Related