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

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

问题描述

这是我第一次使用网络抓取.到目前为止,我能够导航并找到我想要的 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.

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

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