Reputation: 57
I've been teaching myself web scraping recently and I'm having some trouble with an error when trying to make my code modular. I'm getting an error of:
File "ValueScraper.py", line 16, in <module>
table = ts.lol_table(soup)
TypeError: 'list' object is not callable
I'm trying to write a new program that uses the methods of the original program, and can't figure out why this is failing. Here's the old program with all the methods:
from bs4 import BeautifulSoup
from urllib2 import urlopen
import csv
import pandas
#url of Basketball Reference page
url = "http://www.basketball-reference.com/leagues/NBA_2017_totals.html"
#set columns for pandas dataframe
header = ["Player", "Pos", "Age", "Tm", "G", "GS", "MP", "FG", "FGA", "FG%", "3P", "3PA", "3P%", "2P", "2PA", "2P%", "eFG%", "FT", "FTA", "FT%", "ORB", "DRB", "TRB", "AST", "STL", "BLK", "TOV", "PF", "PTS"]
#open url and turn to BS object
def make_soup(url):
html = urlopen(url).read()
soup = BeautifulSoup(html, "lxml")
return soup
#returns list of lists (lol_table) of html table
def lol_table(soup, class_name = ""):
rows = []
if class_name is "":
rows = soup.find_all('tr')
else:
rows = soup.find_all('tr', class_ = class_name)
data = []
for row in rows:
cols = row.find_all('td')
data_row = []
for col in cols:
data_row.append(col.find(text=True))
data.append(data_row)
return data
#create pandas dataframe from lol_table and create csv of it
def to_pandas_csv(lol_table):
df = pandas.DataFrame(lol_table, columns=header)
df.to_csv("nba.csv")
return df
soup = make_soup(url)
lol_table = lol_table(soup, "full_table")
data_frame = to_pandas_csv(lol_table)
And here's the new file:
from bs4 import BeautifulSoup
from urllib2 import urlopen
import csv
import pandas
import TableScraper as ts
#url of table with values
url = "http://www.rotowire.com/daily/NBA/optimizer.php?site=FanDuel"
#columns for table
columns = ["Player", "Value"]
#make soup of url
soup = ts.make_soup(url)
table = ts.lol_table(soup)
Any help would be appreciated.
Upvotes: 0
Views: 179
Reputation: 49330
lol_table = lol_table(soup, "full_table")
Don't rebind that function's name to its call's result. Python does not distinguish between the names of function objects and the names of non-function objects: there can only be one object of that name in that scope, and the most recent binding takes precedence. Choose a different name.
result = lol_table(soup, "full_table")
Upvotes: 4