python - BeautifulSoup 从 find_all 获取文本

标签 python beautifulsoup urllib

这是我的第一个网络抓取工作。到目前为止,我能够导航并找到我想要的 HTML 部分。我也可以打印它。问题是只打印文本,这是行不通的。尝试时出现以下错误:AttributeError: 'ResultSet' object has no attribute 'get_text'

这里是我的代码:

from bs4 import BeautifulSoup
import urllib

page = urllib.urlopen('some url')


soup = BeautifulSoup(page)
zeug = soup.find_all('div', attrs={'class': 'fm_linkeSpalte'}).get_text()


print zeug

最佳答案

find_all() 返回一个元素数组。您应该浏览所有这些并选择您需要的那个。而不是调用 get_text()

UPD
例如:

    for el in soup.find_all('div', attrs={'class': 'fm_linkeSpalte'}):
        print el.get_text()

但请注意,您可能有多个元素。

关于python - BeautifulSoup 从 find_all 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21997587/

相关文章:

Python Selenium 无法从 mpob 网站检索标签内容

python - python中的Soup.select方法 - 如何只选择一个分数?

python - beautifulsoup:bs4.element.ResultSet 对象或列表上的 find_all?

python 从 web URL 读取文件

python - 如何让python成功从互联网上下载大图片

python - 搜索用户输入

python - 如何解决python shopify api InvalidURL : nonnumeric port: Error

python - 类内的 Nose 测试生成器

python - 需要使用 python 登录网站的帮助

python - 如何在google colaboratory中上传数据集?