简体   繁体   中英

How do I iterate a list of values into a string?

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.

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.

将每个响应附加到这样的一个列表中,并从 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}}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM