PRETTIFY() :Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not callable

890 views
Skip to first unread message

Drew Loggi

unread,
Jul 4, 2015, 7:36:08 AM7/4/15
to beauti...@googlegroups.com
Hello All,

Not sure if my post made it up on this forum so I am going to do it one more time to ensure I can get my question answered.

Again, I am using python 3.4.3 and BeautifulSoup4 and when I attempt to use the prettify() code command I get a syntax error as you will specifically see below. Some things of note, however, are that I have used other beautifulsoup commands and I do not incur any syntax errors. For example in doing 'soup.title' and 'soup.title.name', etc, however, I have tried several different approaches and I cannot run the " print(soup.prettify()) " and not get the:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

Again, as you will see below. 


>>> import requests
>>> from bs4 import BeautifulSoup
>>>
>> r.content
>>>
>>>NOTE: THIS IS WHERE ALL THE JUMBLED TOGETHER HTML CODE FROM THE PAGE IN THE ABOVE URL REQUEST.GET IS LISTED
>>>
>>> soup = BeautifulSoup(r.content)
>>> print(soup.prettiy())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
>>>

I have been banging my head against a wall for over a week now and really need some help if someone would be so kind :).

Again, I am a newbie to python and beautifulsoup and really want to get past these hurdles.

I cannot thank all of you enough for any assistance relating to this issue I am having.

Drew Loggi

Leonard Richardson

unread,
Jul 4, 2015, 8:01:27 AM7/4/15
to beauti...@googlegroups.com
Drew,
 
In your example code you wrote soup.prettiy() instead of soup.prettify(). This tells Beautiful Soup to look for a tag called 'prettiy'. It returns None, since there is no such tag, and then calling None() triggers the error you see.
 
Changing 'prettiy' to 'prettify' makes your code work for me. If you still have problems I would make sure you're using Beautiful Soup 4.4.0 and then send another email to the list.
 
Leonard
--
You received this message because you are subscribed to the Google Groups "beautifulsoup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beautifulsou...@googlegroups.com.
To post to this group, send email to beauti...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Drew Loggi

unread,
Jul 6, 2015, 8:58:18 PM7/6/15
to beauti...@googlegroups.com, leon...@segfault.org
Leonard,

My God you are a wonderful person for getting back to me so quickly. 

That being said I think I actually had been typing in the correct spelling other times and this is what is happening..Let's just assume that after I do the 'r.content' and do the large jumbled listing of html I'll list the code I did.

I do have BeautifulSoup 4.4.0 but just to be sure I downloaded it again, and installed it and from the command prompt typed upgrade..the message that came back said I had the most up to date version

Anyway, to provide you the code I got once I used the correct spelling:

C:\Users\Drew>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> from bs4 import BeautifulSoup
>>>
>>> r.content

****JUMBLED LISTING OF HTML ***


>>> soup = BeautifulSoup(r.content)
>>> print soup.prettify()
  File "<stdin>", line 1
    print soup.prettify()
             ^
SyntaxError: invalid syntax
>>> print(soup.prettify())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2026' in position
74904: character maps to <undefined>
>>>


Leonard, thank you so much for your help. I want so badly to do this and to expand my skills with this language and scraping and I would hate to think I got knocked back from the beginning. 

Hope to hear from you very soon.

Drew

Leonard Richardson

unread,
Jul 7, 2015, 3:49:42 PM7/7/15
to beautifulsoup
Drew,
 
>>> soup = BeautifulSoup(r.content)
>>> print soup.prettify()
  File "<stdin>", line 1
    print soup.prettify()
             ^
SyntaxError: invalid syntax
>>> print(soup.prettify())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2026' in position
74904: character maps to <undefined>
>>>
 
In this example, prettify() is working fine. If you just do "pretty = foo.prettify()" you'll see that it runs without crashing. But prettify() returns a Unicode string, and you're having a problem printing Unicode to your terminal.
 
In other words, your problem is with print():
 
 
I don't have a Windows computer, so I can't solve your exact problem, but here are a number of people dealing with similar problems:
 
 
Leonard

Drew Loggi

unread,
Jul 7, 2015, 5:08:24 PM7/7/15
to beauti...@googlegroups.com, leon...@segfault.org
Leonard

Thank you so much my man..just a quick note..I can use the print command on just about any other type of command except for this and that is why this is all so confusing. I am going to try and do this command you suggested and we'll see if this works.

I will let you know..

Jithin j

unread,
Jul 27, 2018, 7:03:02 AM7/27/18
to beautifulsoup
I too am stuck with a code very similar to Drew and getting the same unicode encode error. This is my code:

 
 import urllib.request as url


from bs4 import BeautifulSoup





htmlOnpage=url.urlopen(webpage)

soup=BeautifulSoup(htmlOnpage,'html.parser')

print(soup.prettify())

and getting the error as:

 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 9608-9613: character maps to <undefined>


if the problem is with the print,then what can be done to replace it

I too am a beginner struggling to learn python so  please help me..

Malik Rumi

unread,
Jul 27, 2018, 10:47:20 AM7/27/18
to beauti...@googlegroups.com
Here are the docs you need, depending on which version of Python you are using. Pay especial attention to examples and instruction that tell you to use a kwarg (key word argument) such as encoding='utf-8'. If the docs are too dense, read them anyway, then use what you find there to do a better Google search for simpler instructions. 




“None of you has faith until he loves for his brother or his neighbor what he loves for himself.”


--
You received this message because you are subscribed to the Google Groups "beautifulsoup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beautifulsou...@googlegroups.com.
To post to this group, send email to beauti...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages