-2

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)
luk2302
  • 55,258
  • 23
  • 97
  • 137
Dany Frum
  • 1
  • 1
  • 1
  • 1
    Print `response.content`, check if the element you want to find is actually there. – Haytam Nov 05 '21 at 21:07
  • soup cannot find the object you are looking for. – luk2302 Nov 05 '21 at 21:08
  • Are you perhaps trying to find an element that is created by Javascript? – o11c Nov 05 '21 at 21:15
  • 1
    Does this answer your question? [Getting 'NoneType' object has no attribute 'text' error with BeautifulSoup](https://stackoverflow.com/questions/60690857/getting-nonetype-object-has-no-attribute-text-error-with-beautifulsoup) – Tomerikoo Nov 05 '21 at 23:05

3 Answers3

1

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

mino159
  • 11
  • 1
0

The element you are asking for is not there.

LuckElixir
  • 306
  • 3
  • 14
0

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>]
>>>
mrvol
  • 2,575
  • 18
  • 21