-
Notifications
You must be signed in to change notification settings - Fork 0
/
.py
131 lines (99 loc) · 3.38 KB
/
.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 24 11:48:24 2022
@author: msyed
"""
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 23 11:24:52 2022
@author: msyed
"""
import requests
import pandas as pd
from bs4 import BeautifulSoup
def getFinancialInformation(symbol):
url = "https://finance.yahoo.com/quote/"+symbol+"?p="+symbol
#prop = "Stock market crash"
response = requests.get(url)
t = response.text
soup = BeautifulSoup(t, features = 'html.parser')
#trs = soup.find_all('td', class_ = "Va(t) Fz(14px) Whs(nw) Ta(end) Pstart(10px) Py(6px)")
trs = soup.find_all('tr')
#print(trs[0].find('td', attrs = { "class":"gray-placeholder W(100%) H(30px) My(6px)"}))# class="gray-placeholder W(100%) H(30px) My(6px)"
#print(trs[0].contents[1].text)
#print(trs[0])
finalName = 'Avg. Volume'
names = []
values = []
namVal = {}
#dict2 = {"names": names, "values":values}
for i in range(len(trs)):
for j in range(len(trs[i].contents)):
if j==0: #Name
try:
name = trs[i].contents[j].text
names.append(name)
except:
continue
if j==1:#value
try:
value = trs[i].contents[j].text
values.append(value)
except:
pass
namVal[name]=value
if name ==finalName:
break
return names, values
# print(names)
# print(values)
# print(namVal)
def getCompanyList():
wikiUrl = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
r = requests.get(wikiUrl)
pageText = r.text
soup = BeautifulSoup(pageText, features = 'html.parser')
tickerSymbols = []
values = []
namVal = {}
tbody = soup.find_all('tbody')
#print(tbody[0].contents[6].contents[1].text)
for i in range(len(tbody[0].contents)):
if i<2:
continue
if i%2 !=0:
continue
symbol = tbody[0].contents[i].contents[1].text
tickerSymbols.append(symbol.strip("\n"))
if len(tickerSymbols)==505:
break
return tickerSymbols
data = {"symbol":[],
"metric":[],
"value":[]}
tickerSymbols = getCompanyList()
for symbol in tickerSymbols:
names, values = getFinancialInformation(symbol)
#itearte over vales too
# for name in range(len(names)):
# if name not in data:
# data[name]=[]
# data[name].append(value)
for i in range(len(names)):
data['symbol'].append(symbol)
data['metric'].append(names[i])
data['value'].append(values[i])
# data['symbol']+=[symbol]*len(names)
# data['metric']+=names
# data['values']+=values
# values, names = getCompanyList(symbol)
# #iterate over values too
# for name in range(len(names)):
# name = names[i]
# value = values[i]
# if name not in data:
# data[name]=[]
# data[name].append(value)
# print(tickerSymbols)
df = pd.DataFrame(data)
df.to_csv('financialData.csv')