Issue
I am scraping financial summary from https://www.investing.com/equities/nvidia-corp-financial-summary. Code:
To get the ratio descriptions:
for element in soup.find_all('span', attrs={'class': 'float_lang_base_1'}):
print(element)
The code will result in:
<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>
To get the values for each of ratio above:
for element in soup.find_all('span', attrs={'class': 'float_lang_base_2 text_align_lang_base_2 dirLtr bold'}):
a = element.get_text()
results in:
60.45%
31.47%
26.03%
22.86%
2.95
3.62
-
49.02%
-
-
16.77%
Now, I need to match the two, so that it will be a key value pair that can be transformed into a dataframe.
Gross margin : 60.45%
Operating margin: 31.47%
Net Profit margin: 26.03%
...
Solution
You can find main div
tag which has both the values and iterate over that to identify other properties using class
and append to 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
Here you can use pandas
to create df
and transform dict1
to table form data
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%
.....
Answered By - Bhavya Parikh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.