来自find_all的BeautifulSoup get_text [英] BeautifulSoup get_text from find_all

查看:98
本文介绍了来自find_all的BeautifulSoup get_text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次进行网络抓取.到目前为止,我已经能够导航并找到所需的HTML部分.我也可以打印.问题是仅打印文本,这将不起作用.尝试时出现以下错误:AttributeError: 'ResultSet' object has no attribute 'get_text'

This is my first work with web scraping. So far I am able to navigate and find the part of the HTML I want. I can print it as well. The problem is printing only the text, which will not work. I get following error, when trying it: 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()

find_all() returns an array of elements. You should go through all of them and select that one you are need. And than call get_text()

UPD
例如:

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

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

But note that you may have more than one element.

这篇关于来自find_all的BeautifulSoup get_text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆