BeautifulSoup .text method error: ResultSet object has no attribute 'text'

BeautifulSoup .text method error: ResultSet object has no attribute 'text'

我创建了一个简单的抓取工具,但我在将 ResultSet 更改为文本时遇到问题。我想获得唯一没有 href 等的文本。当我使用 find 方法时效果很好,但是当我添加 find_all 作为第二种方法时它显示错误:

numberone = soup.find("span", itemprop="house").text                      <---- this works good
print(numberone)

numbertwo = soup.find("div", class_="interesting").find_all('a').text     <---- this does not work
print(numbertwo)

'numberone' 的输出很好,但 'numbertwo' 的输出显示错误:

"ResultSet object has no attribute '%s'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?" % key AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

也许有人知道解决方案?

因为它returns一组结果,为了打印所有结果你可以使用这样的for循环:

results = numbertwo = soup.find("div", class_="interesting").find_all('a')
for result in results:
    print(result.text)