简体   繁体   English

如何将值列表迭代为字符串?

[英]How do I iterate a list of values into a string?

I am new to Python so please excuse my lack of understanding.我是 Python 新手,所以请原谅我缺乏理解。

I am able to download data for aapl using BeatifulSoup4, assign the data to specific variables and create a dictionary with the variables.我可以使用 BeatifulSoup4 下载aapl的数据,将数据分配给特定变量并使用变量创建字典。 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.不过,我想做的是通过网站链接迭代stock列表,并为列表中的不同公司多次运行相同的代码,并最终为每个公司提供一个字典。

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 :它为aapl输出以下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.以上是原代码,我想替代aaplsauce中另一个字符串值stocks ,最终得到多个字典如下图所示,这样我可以通过只输入他们自动为各个公司的数据下载到3个不同的字典在stock清单中。

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 .和输出应该是3点字典与名称aaplnvdaamgn

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'] ?您想对stocks = ['aapl', 'nvda', 'amgn']每个条目进行一个请求和处理吗?

If so, in Python you can iterate over a list or dict with a for loop like so:如果是这样,在 Python 中,您可以使用 for 循环遍历列表或字典,如下所示:

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.使用 dict 而不是列表并将处理过的数据存储在其中。

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至于 url 字符串,我使用了litteral string 插值方法

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}}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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