【问题标题】:Convert class 'method' into list in Python在 Python 中将类“方法”转换为列表
【发布时间】:2021-12-12 17:10:19
【问题描述】:

我想将 BeautifulSoup 的文本转换为列表。我确实喜欢这样:

soup = BeautifulSoup(response.text, "html.parser")
story = soup.find("div", attrs={'class': "expandbook__resume"})
story = story.get_text
story = list(story)

但它说“TypeError: 'method' object is not iterable”

【问题讨论】:

  • get_text 是一个方法,所以调用它就可以得到结果:story = story.get_text()
  • 我想从中替换特定的单词,如下所示:story = story.replace("Plus", "")

标签: python converters


【解决方案1】:

查看the BeautifulSoup documentation 其中指出 get_text 是一种方法,应该调用 (get_text())。

soup = BeautifulSoup(response.text, "html.parser")
story = soup.find("div", attrs={'class': "expandbook__resume"})
story = story.get_text()
story = list(story)

【讨论】:

    猜你喜欢
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2010-12-25
    • 2017-12-16
    • 2011-12-05
    • 1970-01-01
    相关资源
    最近更新 更多