Reputation: 1637
I am new to Python so please excuse my lack of understanding.
I am able to download data for aapl
using BeatifulSoup4, assign the data to specific variables and create a dictionary with the variables. What I would like to do though is to iterate the stock
list through the website link and run the same code multiple times for different companies in the list and end up with a dictionary for each company.
import bs4 as bs
import urllib.request
stocks = ['aapl', 'nvda', 'amgn']
sauce = urllib.request.urlopen('https://www.zacks.com/stock/quote/aapl/balance-sheet')
soup = bs.BeautifulSoup(sauce,'lxml')
#Cash & Cash Equivalents
cash_and_equivalents2017 = soup.find_all('td')[33].string
cash_and_equivalents2017 = int(cash_and_equivalents2017.replace(',',''))
cash_and_equivalents2016 = soup.find_all('td')[34].string
cash_and_equivalents2016 = int(cash_and_equivalents2016.replace(',',''))
#Receivables
receivables2017 = soup.find_all('td')[40].string
receivables2017 = int(receivables2017.replace(',',''))
receivables2016 = soup.find_all('td')[41].string
receivables2016 = int(receivables2016.replace(',',''))
aapl = {'Cash & Cash Equivalents':
{'2017': cash_and_equivalents2017,
'2016': cash_and_equivalents2016},
'Receivables':
{'2017': receivables2017,
'2016': receivables2016}
}
print(aapl)
Which outputs the following for aapl
:
{'Cash & Cash Equivalents': {'2017': 74181, '2016': 67155}, 'Receivables': {'2017': 29299, '2016': 30343}}
The above is the original code and I would like to substitute aapl
in sauce
for the other string values in stocks
to eventually get multiple dictionaries as shown below so that I am able to automatically download data for various companies into 3 different dictionaries by just inputting them in the stock
list.
sauce = urllib.request.urlopen('https://www.zacks.com/stock/quote/[stocks]/balance-sheet')
[stocks] = {'Cash & Cash Equivalents':
{'2017': cash_and_equivalents2017,
'2016': cash_and_equivalents2016},
'Receivables':
{'2017': receivables2017,
'2016': receivables2016}
And the output should be 3 dictionaries with the names aapl
, nvda
, amgn
.
aapl = {'Cash & Cash Equivalents':
{'2017': cash_and_equivalents2017,
'2016': cash_and_equivalents2016},
'Receivables':
{'2017': receivables2017,
'2016': receivables2016}
nvda = {'Cash & Cash Equivalents':
{'2017': cash_and_equivalents2017,
'2016': cash_and_equivalents2016},
'Receivables':
{'2017': receivables2017,
'2016': receivables2016}
amgn = {'Cash & Cash Equivalents':
{'2017': cash_and_equivalents2017,
'2016': cash_and_equivalents2016},
'Receivables':
{'2017': receivables2017,
'2016': receivables2016}
Thank you for time and help, it is much appreciated.
Upvotes: 1
Views: 72
Reputation: 788
append each response into one list like this and upack the values from dictioanry:
aapl, nvda, amgn = [{'Cash & Cash Equivalents': {'2017': 74181, '2016': 67155}, 'Receivables': {'2017': 29299, '2016': 30343}},{'Cash & Cash Equivalents': {'2017': 74181, '2016': 67155}, 'Receivables': {'2017': 29299, '2016': 30343}},{'Cash & Cash Equivalents': {'2017': 74181, '2016': 67155}, 'Receivables': {'2017': 29299, '2016': 30343}}]
Upvotes: 0
Reputation: 739
I am not sure if I correctly understood your question.
Do you want to make one request and processing for each entry in stocks = ['aapl', 'nvda', 'amgn']
?
If so, in Python you can iterate over a list or dict with a for loop like so:
for stock in stocks:
# Do your processing here
What you could do in your case is the following:
Use a dict instead of a list and store your processed datas in it.
stocks = {'aapl': {}, 'nvda': {}, 'amgn': {}}
for stock in stocks:
sauce = urllib.request.urlopen(
f'https://www.zacks.com/stock/quote/{stock}/balance-sheet')
soup = bs.BeautifulSoup(sauce, 'lxml')
# Cash & Cash Equivalents
cash_and_equivalents2017 = soup.find_all('td')[33].string
cash_and_equivalents2017 = int(cash_and_equivalents2017.replace(',', ''))
cash_and_equivalents2016 = soup.find_all('td')[34].string
cash_and_equivalents2016 = int(cash_and_equivalents2016.replace(',', ''))
# Receivables
receivables2017 = soup.find_all('td')[40].string
receivables2017 = int(receivables2017.replace(',', ''))
receivables2016 = soup.find_all('td')[41].string
receivables2016 = int(receivables2016.replace(',', ''))
stocks[stock] = {'Cash & Cash Equivalents':
{'2017': cash_and_equivalents2017,
'2016': cash_and_equivalents2016},
'Receivables':
{'2017': receivables2017,
'2016': receivables2016}
}
print(stocks)
As for the url string, I used the litteral string interpolation method
Will output the following:
{
'aapl':
{
'Cash & Cash Equivalents':
{
'2017': 'some value',
'2016': 'some value'
},
'Receivables':
{
'2017': 'some value',
'2016': 'some value'
}
},
'nvda':
{
'Cash & Cash Equivalents':
{
'2017': 'some value',
'2016': 'some value'
},
'Receivables':
{
'2017': 'some value',
'2016': 'some value'
}
},
'amgn':
{
'Cash & Cash Equivalents':
{
'2017': 'some value',
'2016': 'some value'
},
'Receivables':
{
'2017': 'some value',
'2016': 'some value'
}
}
}
Have a nice day.
Upvotes: 1