Funkeh-Monkeh
Funkeh-Monkeh

Reputation: 661

TypeError: 'ResultSet' object is not callable - Python with BeautifulSoup

New to python here and keep running into an error when trying to set up some code to scrape data off a list of web pages.

The link to one of those pages is - https://rspo.org/members/2.htm and I am trying to grab the information on there like 'Membership Number', 'Category', 'Sector', 'Country', etc and export it all into a spreadsheet.

Code:

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
import requests

pages = []

for i in range(1, 10):
    url = 'https://rspo.org/members/' + str(i)
    pages.append(url)


for item in pages:
    page = requests.get(item)
    soup = soup(page.text, 'html.parser')
    member = soup.find_all("span", {"class":"current"})

And I get the following error:

Traceback (most recent call last):

File "", line 3, in soup = soup(page.text, 'html.parser')

TypeError: 'ResultSet' object is not callable

Not sure why I am getting this error. I tried looking at other pages on Stack Overflow but nothing seemed to have a similar error to the one I get above.

Upvotes: 0

Views: 8175

Answers (1)

Brandon Barney
Brandon Barney

Reputation: 2392

The problem is that you have a name conflict because you are using the same name in multiple ways. Thus, your soup sets to a BeautifulSoup soup object, but is then reused as this same object.

Try this instead:

from bs4 import BeautifulSoup
from urllib.request import urlopen
import requests

pages = []

for i in range(1, 10):
    url = 'https://rspo.org/members/' + str(i)
    pages.append(url)


for item in pages:
    page = requests.get(item)
    soup = BeautifulSoup(page.text, 'html.parser')
    member = soup.find_all("span", {"class":"current"})

Note that I just removed the alias from BeautifulSoup. The reason why I took this approach is simple. The standard convention in Python is that classes should be proper case. I.e ClassOne and BeautifulSoup. Instances of classes should be lower-case i.e class and soup. This helps avoid name conflicts, but it also makes your code more intuitive. Once you learn this, it becomes much easier to read code, and to write clean code.

Upvotes: 6

Related Questions