Advertisement
About80Ninjas

getStonks.py

Feb 13th, 2021
1,858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. # In[20]:
  5.  
  6.  
  7. import re
  8. import json
  9. import csv
  10. from io import StringIO
  11. from bs4 import BeautifulSoup
  12. import requests
  13.  
  14.  
  15. # In[21]:
  16.  
  17.  
  18. url_stats = 'https://finance.yahoo.com/quote/{}/key-statistics?p={}'
  19. url_profile = 'https://finance.yahoo.com/quote/{}/profile?p={}'
  20. url_financials = 'https://finance.yahoo.com/quote/{}/financials?p={}'
  21.  
  22.  
  23. # In[46]:
  24.  
  25.  
  26. stock = 'GME'
  27. print("###################################################################################")
  28. print("Collicting info for {}".format(stock))
  29.  
  30.  
  31. # In[23]:
  32.  
  33.  
  34. responce = requests.get(url_financials.format(stock, stock))
  35.  
  36.  
  37. # In[24]:
  38.  
  39.  
  40. soup = BeautifulSoup(responce.text, 'html.parser')
  41.  
  42.  
  43. # In[25]:
  44.  
  45.  
  46. pattern = re.compile(r'\s--\sData\s--\s')
  47. script_data = soup.find('script', text=pattern).contents[0]
  48.  
  49.  
  50. # In[26]:
  51.  
  52.  
  53. start = script_data.find("context")-2
  54.  
  55.  
  56. # In[27]:
  57.  
  58.  
  59. json_data = json.loads(script_data[start:-12])
  60.  
  61.  
  62. # # Financial data
  63.  
  64. # In[28]:
  65.  
  66.  
  67. annual_is = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['incomeStatementHistory']['incomeStatementHistory']
  68. quarterly_is = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['incomeStatementHistoryQuarterly']['incomeStatementHistory']
  69.  
  70. annual_cf = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['cashflowStatementHistoryQuarterly']['cashflowStatements']
  71. quarterly_cf = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['cashflowStatementHistory']['cashflowStatements']
  72.  
  73. annual_bs = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['balanceSheetHistory']['balanceSheetStatements']
  74. quarterly_bs = json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['balanceSheetHistoryQuarterly']['balanceSheetStatements']
  75.  
  76.  
  77. # In[29]:
  78.  
  79.  
  80. annual_is_stats = []
  81. quarterly_is_stats = []
  82. annual_cf_stats = []
  83. quarterly_cf_stats = []
  84. annual_bs_stats =[]
  85. quarterly_bs_stats = []
  86.  
  87.  
  88. for s in annual_is:
  89.     statement = {}
  90.     for key, val in s.items():
  91.         try:
  92.             statement[key] = val['raw']
  93.         except TypeError:
  94.             continue
  95.         except KeyError:
  96.             continue
  97.     annual_is_stats.append(statement)
  98.    
  99. for s in quarterly_is:
  100.     statement = {}
  101.     for key, val in s.items():
  102.         try:
  103.             statement[key] = val['raw']
  104.         except TypeError:
  105.             continue
  106.         except KeyError:
  107.             continue
  108.     quarterly_is_stats.append(statement)
  109.    
  110. for s in annual_cf:
  111.     statement = {}
  112.     for key, val in s.items():
  113.         try:
  114.             statement[key] = val['raw']
  115.         except TypeError:
  116.             continue
  117.         except KeyError:
  118.             continue
  119.     annual_cf_stats.append(statement)
  120.    
  121. for s in quarterly_cf:
  122.     statement = {}
  123.     for key, val in s.items():
  124.         try:
  125.             statement[key] = val['raw']
  126.         except TypeError:
  127.             continue
  128.         except KeyError:
  129.             continue
  130.     quarterly_cf_stats.append(statement)
  131.    
  132. for s in annual_bs:
  133.     statement = {}
  134.     for key, val in s.items():
  135.         try:
  136.             statement[key] = val['raw']
  137.         except TypeError:
  138.             continue
  139.         except KeyError:
  140.             continue
  141.     annual_bs_stats.append(statement)
  142.    
  143. for s in quarterly_bs:
  144.     statement = {}
  145.     for key, val in s.items():
  146.         try:
  147.             statement[key] = val['raw']
  148.         except TypeError:
  149.             continue
  150.         except KeyError:
  151.             continue
  152.     quarterly_bs_stats.append(statement)
  153.  
  154.  
  155. # In[39]:
  156.  
  157.  
  158. print("###################################################################################")
  159. print("Showing annual income Statement History")
  160. print(annual_is_stats[0])
  161.  
  162.  
  163. # In[40]:
  164.  
  165.  
  166. print("###################################################################################")
  167. print("Showing quarterly income Statement History")
  168. print(quarterly_is_stats[0])
  169.  
  170.  
  171. # In[41]:
  172.  
  173.  
  174. print("###################################################################################")
  175. print("Showing annual cash flow Statements")
  176. print(annual_cf_stats[0])
  177.  
  178.  
  179. # In[42]:
  180.  
  181.  
  182. print("###################################################################################")
  183. print("Showing quarterly cash flow Statements")
  184. print(quarterly_cf_stats[0])
  185.  
  186.  
  187. # In[43]:
  188.  
  189.  
  190. print("###################################################################################")
  191. print("Showing annual balance Sheet Statements")
  192. print(annual_bs_stats[0])
  193.  
  194.  
  195. # In[44]:
  196.  
  197.  
  198. print("###################################################################################")
  199. print("Showing quarterly balance Sheet Statements")
  200. print(quarterly_bs_stats[0])
  201.  
  202.  
  203. # # Profile data
  204.  
  205. # In[30]:
  206.  
  207.  
  208. responce = requests.get(url_profile.format(stock, stock))
  209. soup = BeautifulSoup(responce.text, 'html.parser')
  210. pattern = re.compile(r'\s--\sData\s--\s')
  211. script_data = soup.find('script', text=pattern).contents[0]
  212. start = script_data.find("context")-2
  213. json_data = json.loads(script_data[start:-12])
  214.  
  215.  
  216. # In[31]:
  217.  
  218.  
  219. print("###################################################################################")
  220. print("Showing company Officers")
  221. print(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['companyOfficers'])
  222.  
  223.  
  224. # In[32]:
  225.  
  226.  
  227. print("###################################################################################")
  228. print('Showing long Business Summary')
  229. print(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['assetProfile']['longBusinessSummary'])
  230.  
  231.  
  232. # In[33]:
  233.  
  234.  
  235. # Filtes for K8 and K10 sec filings
  236. filings =[]
  237. for f in json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['secFilings']['filings']:
  238.     if f['type'] == '8-K' or f['type'] == '10-K':
  239.         filings.append(f)
  240.  
  241.  
  242. # In[34]:
  243.  
  244.  
  245. print("###################################################################################")
  246. print("Showing 8-K and 10-K SEC filings")
  247. print(filings)
  248.  
  249.  
  250. # # Statistics
  251.  
  252. # In[35]:
  253.  
  254.  
  255. responce = requests.get(url_stats.format(stock, stock))
  256. soup = BeautifulSoup(responce.text, 'html.parser')
  257. pattern = re.compile(r'\s--\sData\s--\s')
  258. script_data = soup.find('script', text=pattern).contents[0]
  259. start = script_data.find("context")-2
  260. json_data = json.loads(script_data[start:-12])
  261.  
  262.  
  263. # In[36]:
  264.  
  265.  
  266. print("###################################################################################")
  267. print("Showing Key Statistics")
  268. print(json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['defaultKeyStatistics'])
  269.  
  270.  
  271. # In[18]:
  272.  
  273.  
  274. stock_url = 'https://query1.finance.yahoo.com/v7/finance/download/{}?'
  275.  
  276. params = {
  277.     'range': '5y',
  278.     'interval': '1d',
  279.     'events': 'history',
  280.     'includeAdjustedClose': 'true'
  281. }
  282.  
  283. responce = requests.get(stock_url.format(stock), params=params)
  284.  
  285.  
  286. # In[19]:
  287.  
  288.  
  289. print("###################################################################################")
  290. print("Showing history 5y 1d interval")
  291. file = StringIO(responce.text)
  292. reader = csv.reader(file)
  293. data = list(reader)
  294. for row in data[:5]:
  295.     print(row)
  296.  
  297.  
  298. # In[ ]:
  299.  
  300.  
  301.  
  302.  
  303.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement