Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Error – AttributeError: 'int' object has no attribute 'find'

=====Functioning code=====

(Omission)

contents = soup.find('table').find_all('a')


for i in contents:            
        print("---------------------------")
        link = i.find("td", class_= "cafecoffee").find_all("a")[0]
        print("link :")
        print("naver.com" + link)

        title = i.find("td")
        print("title:",title.text)

=====Non-functioning code=====

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(Omission)

contents = soup.find('table').find_all('a')


for i in range(1,52): # <<<<changed
        print("---------------------------")
        link = i.find("td", class_= "cafecoffee").find_all("a")[0]
        print("link :")
        print("naver.com" + link)

        title = i.find("td")
        print("title:",title.text)

I don’t know what the problem is, could you please help me, seniors?

I haven’t made any attempts. It’s only been an hour since I learned the language.

>Solution :

The AttributeError: ‘int’ object has no attribute ‘find’ tells you exactly what the problem is:

An ‘int’ object has no attribute ‘find’.

Now you can ask yourself the question which WORD in this statement you don’t understand and try to find a definition for this word. Is it object you don’t understand? is it attribute? Is it has no? Is it int? Is it find?

As a beginner it is worth to know about the importance of naming the used variables.

The name i suggests for example usually an integer value ( 0, 1, 2, 3, … ) and the name s suggests a string value ( ‘0’, ‘1’, ‘2’, ‘3’, … ). Choosing a name which does not suggest what the variable with this name actually stores can easily lead to confusion.

for i in contents:

makes out of i a special value being an item in the iterable contents. Such item (called often an ‘object’) comes with the .find() method, so it’s ok to use it, but … the i suggests somehow an integer value what is probably the reason of changing the for loop to:

for i in range(1,52):

and expecting it would work the same way. But it doesn’t. The variable i stores now an integer value and integer values do not come with a .find() method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading