pythonnlplexical

I receive this error 'str' object has no attribute 'text' when I try to run the following code


I had removed .text and had just written peter_pan.split() but I was not sure whether the output was correct.

import requests
from nltk import FreqDist

url = "https://www.gutenberg.org/files/16/16-0.txt"
peter_pan = requests.get(url).text

peter_pan_words = peter_pan.text.split()

word_frequency = FreqDist(peter_pan_words)

freq = word_frequency.most_common(3)[2][1]


print(freq)

Solution

  • Where you are doing:

    peter_pan = requests.get(url).text
    

    The text that you are using is a method of the response object that is returned by requests.get. This method returns a string (containing the contents of the page). All this is fine.

    However, you are then doing:

    peter_pan_words = peter_pan.text.split()
    

    This time what you are trying to do is access the text method of a str object, but a str does not have any method called text. If you do print(dir(peter_pan)), you will see the attributes (methods/properties) that are available for you to access. You should see something similar to:

    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    

    You will see that text is not one of them (which is why you are getting the error), but you will also see that split is there -- and in fact all you need to do is to invoke split directly:

    peter_pan_words = peter_pan.split()