Python错误 – AttributeError: ‘int’对象没有属性’find’

huangapple go评论64阅读模式
英文:

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

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

英文:

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

答案1

得分: 0

AttributeError: 'int' object has no attribute 'find' 告诉您问题的确切原因:

'int' 对象没有 'find' 属性。

现在您可以自己思考这个陈述中哪个词您不理解,并尝试找到该词的定义。您不理解的是 object 吗?还是 attribute?还是 has no?还是 int?还是 find

作为初学者,了解命名所用变量的重要性是值得的。

例如,名称 i 通常暗示整数值(0、1、2、3、...),名称 s 暗示字符串值('0'、'1'、'2'、'3'、...)。选择一个名称,它并不暗示这个名称的变量实际上存储了什么,很容易导致混淆。

for i in contents:

i 变成了可迭代对象 contents 中的一个特殊值(通常称为 'object')。这种项目通常具有 .find() 方法,所以可以使用它,但是... i 暗示了某种整数值,这可能是更改 for 循环为:

for i in range(1, 52):

并期望它能够以相同的方式工作的原因。但它并不会。变量 i 现在存储一个整数值,整数值不具有 .find() 方法。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 12:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定