-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (34 loc) · 1.23 KB
/
main.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
import requests
import csv
def to_csv_format(stocks):
stock_list = [{} for i in range(len(stocks))]
for i in range(len(stocks)):
stock_list[i] = {
"code": stocks[i]["symbolCode"],
"stockName": stocks[i]["stockName"],
"stockNameEng": stocks[i]["stockNameEng"],
"openPrice": None if stocks[i]["openPrice"] == "-" else float(stocks[i]["openPrice"].replace(",","")),
"nationType": stocks[i]["nationType"],
}
return stock_list
def crawling():
stocks = []
for i in range(10):
response = requests.get("https://api.stock.naver.com/stock/exchange/NASDAQ/marketValue", {
"page": 1+(20*i),
"pageSize": 20
})
if response.status_code == 200:
stocks.extend(response.json()["stocks"])
else:
print(f"{response.status_code}: error")
print(f"Foreign Stock's found {len(stocks)}")
stocks_list = to_csv_format(stocks)
with open("foreign_stocks.csv", 'w') as f:
field_names = stocks_list[0].keys()
w = csv.DictWriter(f, fieldnames=field_names)
w.writeheader()
w.writerows(stocks_list)
f.close()
if __name__ == '__main__':
crawling()