[英]Map Multiple Values to Python Dictionary Key from Two Different Lists
[英]How to convert values from two different lists into a single dictionary?
我正在从https://www.investing.com/equities/nvidia-corp-financial-summary中抓取财务摘要。 代码:
要获取比率描述:
for element in soup.find_all('span', attrs={'class': 'float_lang_base_1'}):
print(element)
该代码将导致:
<span class="float_lang_base_1">Gross margin</span>
<span class="float_lang_base_1">Operating margin</span>
<span class="float_lang_base_1">Net Profit margin</span>
<span class="float_lang_base_1">Return on Investment</span>
<span class="float_lang_base_1">Quick Ratio</span>
<span class="float_lang_base_1">Current Ratio</span>
<span class="float_lang_base_1">LT Debt to Equity</span>
<span class="float_lang_base_1">Total Debt to Equity</span>
<span class="float_lang_base_1">Cash Flow/Share</span>
<span class="float_lang_base_1">Revenue/Share</span>
<span class="float_lang_base_1">Operating Cash Flow</span>
要获取上述每个比率的值:
for element in soup.find_all('span', attrs={'class': 'float_lang_base_2 text_align_lang_base_2 dirLtr bold'}):
a = element.get_text()
结果是:
60.45%
31.47%
26.03%
22.86%
2.95
3.62
-
49.02%
-
-
16.77%
现在,我需要将两者进行匹配,这样它将是一个可以转换为 dataframe 的键值对。
Gross margin : 60.45%
Operating margin: 31.47%
Net Profit margin: 26.03%
...
您可以使用class
和 append 找到具有这两个值的主div
标签并对其进行迭代以识别其他属性到dict1
dict1={}
for element in soup.find_all('div', attrs={'class': 'infoLine'}):
name=element.find("span",class_="float_lang_base_1").get_text()
value=element.find("span",class_="float_lang_base_2").get_text()
dict1[name]=value
这里可以使用pandas
创建df
并将dict1
转换为表格数据
import pandas as pd
df=pd.DataFrame(dict1.items(),columns=['A','B'])
df
Output:
A B
0 Gross margin 60.45%
1 Operating margin 31.47%
.....
您可以将两个不同列表中的值获取到单个字典中
Mykeys = ["a", "b", "c"]
Myvalues = [1, 3, 5]
print ("Mykey list: " + str(Mykeys))
print ("Myvalue list: " + str(Myvalues))
res = dict(zip(Mykeys, Myvalues))
print ("New dictionary will be : " + str(res))
如答案中所述,您可以zip()
您的列表并转换为dict()
。
无论如何,从元素中选择和提取信息有一种替代方法:
dict(list(row.stripped_strings)[::len(list(row.stripped_strings))-1] for row in soup.select('.infoLine'))
这将使用 class infoLine
select()
或find_all()
元素<span>
的容器标记是什么。 虽然.stripped_strings
将文本提取为ResultSet
我们只需要list slice
第一个和最后一个元素并将其在dict comprehension
中转换为最终结果。
请注意:压缩列表或使用列表,您必须确保它们具有相同的长度,否则您将收到有关此不匹配的错误。
import requests
from bs4 import BeautifulSoup
url='https://www.investing.com/equities/nvidia-corp-financial-summary'
soup = BeautifulSoup(requests.get(url, headers = {'User-Agent': 'Mozilla/5.0'}).text)
dict(list(row.stripped_strings)[::len(list(row.stripped_strings))-1] for row in soup.select('.infoLine'))
{'Gross margin': '60.45%',
'Operating margin': '31.47%',
'Net Profit margin': '26.03%',
'Return on Investment': '22.86%',
'Quick Ratio': '2.95',
'Current Ratio': '3.62',
'LT Debt to Equity': '-',
'Total Debt to Equity': '49.02%',
'Cash Flow/Share': '-',
'Revenue/Share': '-',
'Operating Cash Flow': '16.77%'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.