我是Python的新手,请原谅我缺乏理解。
我可以使用BeatifulSoup4下载aapl
的数据,将数据分配给特定变量并使用变量创建字典。我想做的是通过网站链接迭代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)
为aapl
输出以下内容:
{'Cash & Cash Equivalents': {'2017': 74181, '2016': 67155}, 'Receivables': {'2017': 29299, '2016': 30343}}
以上是原始代码,我想用aapl
中的sauce
替换stocks
中的其他字符串值,最终获得多个字典,如下所示,以便我能够只需在stock
列表中输入数据,即可自动将各公司的数据下载到3个不同的词典中。
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}
输出应该是3个字典,名称为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}
感谢您的时间和帮助,非常感谢。
答案 0 :(得分:1)
我不确定我是否正确理解了你的问题。
您是否要为for stock in stocks:
# Do your processing here
中的每个条目提出一个请求和处理?
如果是这样,在Python中,您可以使用for循环遍历列表或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)
您可以在以下情况下执行以下操作:
使用dict而不是列表并将已处理的数据存储在其中。
{
'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'
}
}
}
至于url字符串,我使用了litteral string interpolation method
将输出以下内容:
{{1}}
度过愉快的一天。
答案 1 :(得分:0)
将每个响应附加到这样的一个列表中,然后从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}}]