Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeautifulSoup get_text from find_all

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'

Here my code:

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
like image 448
Krytos Avatar asked Feb 24 '14 19:02

Krytos


2 Answers

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 example:

    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.

like image 174
amaslenn Avatar answered Nov 16 '22 22:11

amaslenn


Try for inside the list for getting the data, like this:

zeug = [x.get_text() for x in soup.find_all('div', attrs={'class': 'fm_linkeSpalte'})]
like image 1
Muhtadi Avatar answered Nov 17 '22 00:11

Muhtadi