Home > database >  Python Error - AttributeError: 'int' object has no attribute 'find'
Python Error - AttributeError: 'int' object has no attribute 'find'

Time:02-06

=====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=====

(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.

CodePudding user response:

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.

  • Related