-
Notifications
You must be signed in to change notification settings - Fork 6
/
grabStocks.py
31 lines (25 loc) · 896 Bytes
/
grabStocks.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
import urllib.request
import json
import re
def grab_stocks():
# Download the NASDAQ traded symbols file
url = 'ftp://ftp.nasdaqtrader.com/symboldirectory/nasdaqtraded.txt'
try:
# Download and decode the content
with urllib.request.urlopen(url) as response:
content = response.read().decode('utf-8')
except Exception as e:
raise Exception(f"Failed to download NASDAQ symbols: {str(e)}")
# Extract stock symbols using regex
# Matches patterns like 'A|AAIC' and captures everything after the pipe
pattern = r'^\w\|(\w+)'
symbols = []
for line in content.split('\n'):
match = re.match(pattern, line)
if match:
symbols.append(match.group(1))
# Write to JSON file
with open('stocks.json', 'w') as f:
json.dump(symbols, f)
if __name__ == '__main__':
grab_stocks()