【问题标题】:TypeError: 'ResultSet' object is not callable - pythonTypeError:'ResultSet'对象不可调用-python
【发布时间】:2020-11-29 00:29:30
【问题描述】:

我搜索了其他遇到相同错误的人,但我认为我没有同样的问题。我想获得网站上每个项目的名称,我认为是<h2 class = "heading">。但我不知道为什么会出现此错误,也不知道该怎么做。这是我的代码https://www.jetsetter.com/magazine/cool-tech-gadgets-on-amazon/中的网站网址,

代码:

import requests
from bs4 import  BeautifulSoup

res_item = requests.get('https://www.jetsetter.com/magazine/cool-tech-gadgets-on-amazon/')
bs_item = BeautifulSoup(res_item.text,'html.parser')
list_item = bs_item.find_all('h2',class_ ="heading")

for item_print in list_item():
    print(item_print)

我得到了错误: line 8, in <module> for item_print in list_item(): TypeError: 'ResultSet' object is not callable

当我打印 list_item 时,它也会显示 HTML 代码的某些部分,例如

</h2>, <h2 class="heading">
                    Tile Mate with Replaceable Battery
                </h2>

但我只希望它在第二行显示字符串。

【问题讨论】:

  • 为什么list_item有括号删除()
  • 您试图通过添加() 来调用list_item,但是它不可调用。所以删除()

标签: python beautifulsoup


【解决方案1】:

您的代码中有错字item_print in list_item() 请改用item_print in list_item:

试试这个

import requests
from bs4 import  BeautifulSoup

res_item = requests.get('https://www.jetsetter.com/magazine/cool-tech-gadgets-on-amazon/')
bs_item = BeautifulSoup(res_item.text,'html.parser')
list_item = bs_item.find_all('h2',class_ ="heading")

for item_print in list_item:
    print(item_print.get_text(strip=True))

【讨论】:

    猜你喜欢
    • 2016-08-17
    • 2017-12-20
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2017-05-08
    相关资源
    最近更新 更多