Reputation: 6041
I'm trying to create a dictionary from two arrays of scraped data. Everything in this code works except for the last step, where it throws the following error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 19 tale_data_governors = [data.string for data in table_data_governors] 20 ---> 21 dictionary = dict(zip(table_data_governors,table_data))
TypeError: 'list' object is not callable
#Return the results in dictionary form
#{'Brown': 56.0126582278481, 'Whitman': 43.9873417721519}
html = requests.get("https://www.realclearpolitics.com/epolls/2010/governor/ca/california_governor_whitman_vs_brown-1113.html").text
soup = BeautifulSoup(html, 'html.parser')
#Scrape the percentage Numbers
table = soup.find_all('table')[0]
table_row = table.find_all('tr')[1]
table_data = table_row.find_all('td')[3:5]
#Scrape the Names
table_row_governors = table.find_all('tr')[0]
table_data_governors = table_row_governors.find_all('th')[3:5]
table_data_governors
table_data = [data.string for data in table_data]
tale_data_governors = [data.string for data in table_data_governors]
dictionary = dict(zip(table_data_governors,table_data))
Any help/suggestions would be appreciated!
Thanks
EDIT: I think it may have something to do with this:
[(<th>Brown (D)</th>, u'53.1'), (<th>Whitman (R)</th>, u'41.7')]
This is the array of tuples I get when I call zip(). I'm not quite sure why this happens. I thought that data.string would convert these into strings..
EDIT 2: This code works
a = zip(table_data, table_data_governors)
b = {}
for x,y in a:
b[y] = x
b
Strange stuff. Might have something to do with the Ipython notebook.
Upvotes: 2
Views: 1711
Reputation: 183
There is a typo in your code.
You have defined tale_data_governors
and are calling table_data_governors
later.
Upvotes: 1