-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha35_2_yahoo_repurchase.py
207 lines (152 loc) · 5.43 KB
/
a35_2_yahoo_repurchase.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#%%
from import_basics import *
from bs4 import BeautifulSoup
ticker='QCOM'
#%%
from import_mongo import *
#%%
def repurchase_ratio_analysis(ticker):
url=f'https://finance.yahoo.com/quote/{ticker}/cash-flow'
# url = f'https://finance.yahoo.com/quote/{ticker}/profile/'
headers = {"User-Agent": "insomnia/8.4.2"}
# Make a request to the URL
response = requests.get(url, headers=headers)
# Parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
# Find the div with class 'hello_there'
#//tr[@class="Bxz(bb) Bdbw(1px) Bdbs(s) Bdc($seperatorColor) H(36px) "]
class1="D(tbr) fi-row Bgc($hoverBgColor):h"
div_content = soup.find_all('div', class_=class1)
div_content
div_content
for item in div_content:
if 'repurchase' in item.text.lower():
print("item.text.lower(): ",item.text.lower())
print('here 1')
break
try:
repurchase_total=float(item.find_all('span')[1].text.replace(',',''))
repurchase_total=abs(repurchase_total)
market_cap,industry, short_description,company_name,PE=get_stock_details(ticker)
market_cap=market_cap*1000000
market_cap
repurchase_ratio=repurchase_total/market_cap
print("market_cap: ",market_cap)
dict1=dict(ticker=ticker,repurchase_ratio=repurchase_ratio,market_cap=market_cap,industry=industry, short_description=short_description,company_name=company_name,PE=PE)
except:
dict1=dict(ticker=ticker,repurchase_ratio=0,market_cap=0,industry=0, short_description=0,company_name=0,PE=0)
return dict1
def finviz_df(ticker):
url = "https://finviz.com/quote.ashx"
querystring = {"t":ticker,"p":"d"}
payload = ""
headers = {
"cookie": "chartsTheme=dark; notice-newsletter=show",
"User-Agent": "insomnia/8.4.2"
}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
# print(response.text)
soup = BeautifulSoup(response.text, 'html.parser')
text = soup.text
text
'''
class="js-snapshot-table snapshot-table2 screener_snapshot-table-body"
'''
div_content = soup.find_all('tr', class_="Bxz(bb) Bdbw(1px) Bdbs(s) Bdc($seperatorColor) H(36px) ")
div_content
class_name = "Bxz(bb) Bdbw(1px) Bdbs(s) Bdc($seperatorColor) H(36px)".replace(" ", ".").replace("(", "\(").replace(")", "\)")
# Find all tr elements with the specified class
#//td[@data-test="PE_RATIO-value"]
elements = soup.find_all('tr', class_=class_name)
elements
#//table[@class="snapshot-table2 screener_snapshot-table-body"]
# table = soup.find_all('table')
# table=soup.find_all('table', class_="snapshot-table2 screener_snapshot-table-body")
table=soup.find_all('table', class_="js-snapshot-table snapshot-table2 screener_snapshot-table-body")
print("\n>> len(table)= ", len(table))
table
soup2 = BeautifulSoup(str(table[0]), 'html.parser').find_all('tr')
# print("\n>> len(soup2)= ", len(soup2))
soup2
list1=[]
list2=[]
sum1=0
for i in range(len(soup2)):
cells=soup2[i].find_all('td')
cells
for ii,cell in enumerate(cells):
# print("cell.text: ",cell.text)
if type(cell.text)==type('text'):
text1=cell.text
# text1=text1.replace('/','divided by').replace('.','_')
if ii%2==0:
list1.append(text1)
else:
list2.append(text1)
sum1+=1
list2
de=pd.DataFrame(list2).T
de
de.columns=list1
de
# dividend_rate=float(de['Dividend TTM'][0].split(' ')[1].replace('(','').replace('%)',''))
# dividend_rate=dividend_rate/100
# dividend_rate
# short_interest=de['Short Float / Ratio'][0]
# Beta=de['Beta'][0]
# insider_owned=de['Insider Own'][0]
# insider_trade=de['Insider Trans'][0]
# institut_owned=de['Inst Own'][0]
# institut_trade=de['Inst Trans'][0]
# quick_ratio=de['Quick Ratio'][0]
# Beta
# dict1=dict(short_interest=short_interest,
# Beta=Beta,
# insider_owned=insider_owned,
# insider_trade=insider_trade,
# institut_owned=institut_owned,
# institut_trade=institut_trade,
# quick_ratio=quick_ratio,
# )
de.columns = [col.replace('.', '_') for col in de.columns]
de['ticker']=ticker
columns = list(de.columns)
columns.remove('ticker')
columns.insert(0, 'ticker')
dn = de[columns]
dn
return dn
# %%
stock_list11000=stock_list_11000()
stock_list11000
ticker='QCOM'
# %%
#%%
#%% print("\n>> len(stock_list11000)= ", len(stock_list11000))
#%%
i
#%%
for i,ticker in enumerate(stock_list11000):
if i <3148:
continue
time.sleep(10)
# dn=finviz_df(ticker)
try:
dict1=repurchase_ratio_analysis(ticker)
dict1
if dict1['market_cap']==0:
print(i,'not stock',ticker)
continue
dn1=pd.DataFrame.from_dict(dict1,orient='index').T
dn1
dn=finviz_df(ticker)
dn
dt=dn.merge(dn1,how='outer',on='ticker')
dt
target_column='ticker'
mongo_update_insert_one('stock','finviz_v1',dt,target_column)
print(i,"ticker: ",ticker)
except Exception as e:
content=f'{i}_{ticker}_{e}'
print1(content,filename='error_finviz_v1')
# %%