Home > OS >  AttributeError: 'NoneType' object has no attribute 'text' , I don't underst
AttributeError: 'NoneType' object has no attribute 'text' , I don't underst

Time:11-06

I'm trying to read a file with python and get each line as a parameter for a function. I've got a AttributeError: 'NoneType' object has no attribute 'text' error and I don't understand how to fix it.

from bs4 import BeautifulSoup

from requests_html import HTMLSession

session = HTMLSession()

response = session.get('https://pool.rplant.xyz/#')

soup = BeautifulSoup(response.content, 'html.parser')

nah = soup.find('span', {'id': 'statsPoolMc'}).text

print(nah)

CodePudding user response:

The element you are asking for is not there.

CodePudding user response:

The element is not there. It's probably because soup.find actually found nothing. So you have to edit what you are looking for.

CodePudding user response:

when you do find any elements makes sense to check their existing of them before calling attributes.

Like that:

# nah = soup.find('span', {'id': 'statsPoolMc'}).text
nah = soup.find('span', {'id': 'statsPoolMc'})
if nah:
   nah_text = nah.text
   # do next stuff here

But there is no such element on the page https://pool.rplant.xyz/#:

>>> soup.find_all('span')
[<span tkey="plantpool">Rplant pool</span>, <span id="statsUpdated"><span tkey="statsUpdated">Stats Updated</span>  <i class="fa fa-bolt"></i></span>, <span tkey="statsUpdated">Stats Updated</span>, <span class="noinvert" id="langFlag"><img alt="" data-src="static1/en.png" uk-img="" width="32px"/></span>, <span tkey="darkMode">Dark mode</span>, <span tkey="enableNotify">Enable notifications</span>]
>>>
  • Related