With diagnose I get 'NoneType' object is not callable error

25 views
Skip to first unread message

trynalearn

unread,
Nov 21, 2022, 12:57:30 PM11/21/22
to beautifulsoup
Hi All,

Hopefully someone can help me with this.

I have this short program (I will explain the red below):
-------------------------------------------------------
from bs4 import BeautifulSoup
from bs4.diagnose import diagnose
_filehandle = open("epc_detail_page.html", "r", encoding="UTF-8")
read_text = _filehandle.read()
bs = BeautifulSoup(read_text, "html.parser")
if hasattr(bs, 'read'):
    print("has read")
print("\n\n\n")
diagnose(bs)
-------------------------------------------------------

When it runs I get this output:
-------------------------------------------------------
has read

Diagnostic running on Beautiful Soup 4.11.1
Python version 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)]
Found lxml version 4.9.1.0
Found html5lib version 1.1
Traceback (most recent call last):
  File "c:\Users\trynalearn\Downloads\my_python_code\get_epc_details\beautiful_soup_diagnose.py", line 13, in <module>
    diagnose(bs)
  File "C:\Users\jason\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\bs4\diagnose.py", line 61, in diagnose
    data = data.read()
TypeError: 'NoneType' object is not callable
-------------------------------------------------------

If I look at the diagnose code it says this:
-------------------------------------------------------
if hasattr(data, 'read'):
    data = data.read()
elif data.startswith("http:") or data.startswith("https:"):
-------------------------------------------------------

So my code checks hasattr(bs, 'read') and so does diagnose, but diagnose then throws an error.

Can anyone tell my why this is?  What am I doing wrong?

Many thanks!!
Reply all
Reply to author
Forward
0 new messages