views:

540

answers:

1

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.

+4  A: 

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 ;-)

Adam Bernier
Thanks for the shout out :)
Triptych
Don't mention it: Great work deserves recognition!
Adam Bernier