Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "'ResultSet' has no attribute 'findAll'" using BeautifulSoup in Python?

So I am learning Python slowly, and am trying to make a simple function that will draw data from the high scores page of an online game. This is someone else's code that i rewrote into one function (which might be the problem), but I am getting this error. Here is the code:

>>> from urllib2 import urlopen
>>> from BeautifulSoup import BeautifulSoup
>>> def create(el):
    source = urlopen(el).read()
    soup = BeautifulSoup(source)
    get_table = soup.find('table', {'id':'mini_player'})
    get_rows = get_table.findAll('tr')
    text = ''.join(get_rows.findAll(text=True))
    data = text.strip()
    return data

>>> create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')

Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
  File "<pyshell#17>", line 6, in create
    text = ''.join(get_rows.findAll(text=True))
AttributeError: 'ResultSet' object has no attribute 'findAll'

Thanks in advance.

like image 609
Alex Avatar asked Jun 14 '09 04:06

Alex


1 Answers

Wow. Triptych provided a great answer to a related question.

We can see, from BeautifulSoup's source code, that ResultSet subclasses list.

In your example, get_rows is an instance of BS's ResultSet class,
and since BS's ResultSet subclasses list, that means get_rows is a list.

get_rows, as an instance of ResultSet, does not have a findAll method implemented; hence your error.
What Triptych has done differently is to iterate over that list.
Triptych's method works because the items in the get_rows list are instances of BS's Tag class; which has a findAll method.

So, to fix your code, you could replace the last three lines of your create method with something like this:

for row in get_rows:
    text = ''.join(row.findAll(text=True))
    data = text.strip()
    print data

Note to Leonard Richardson: in no way do I intend to demean the quality of your work by referring to it as BS ;-)

like image 67
mechanical_meat Avatar answered Oct 01 '22 18:10

mechanical_meat