-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rate of Exchange Crawling.py
51 lines (38 loc) · 1.51 KB
/
Rate of Exchange Crawling.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
from matplotlib import pyplot as plt
from urllib.request import urlopen
from bs4 import BeautifulSoup
import json
# settings..
country = "USD" # country you want
Max_page_num = 3 # page_number you want
exchange_list = {}
def crawling(Max_page_num):
page_num = 0
exchange_dict_edit = {}
while True:
URL = "https://finance.naver.com/marketindex/exchangeDailyQuote.nhn?marketindexCd=FX_{0}KRW&page={1}".format(country, page_num)
html = urlopen(URL).read()
soup = BeautifulSoup(html, "html.parser")
data_exchange = []
data_ex_time = []
for x in range(len(soup.find_all(class_="num"))):
if x%2 is 0:
data_exchange.append(float(soup.find_all(class_="num")[x].text.replace(',','')))
for x in range(len(soup.find_all(class_="date"))):
data_ex_time.append(soup.find_all(class_="date")[x].text)
for x in range(len(data_ex_time)):
exchange_dict_edit[data_ex_time[x]] = data_exchange[x]
if page_num == Max_page_num:
return exchange_dict_edit
page_num += 1
def save_json(dic_data):
with open('./json/sample.json', 'w', encoding='utf-8') as f:
json.dump(dic_data, f, indent="\t")
if __name__ == "__main__":
dic_data = crawling(Max_page_num)
save_json(dic_data)
X = [k.split('.')[1]+"/"+k.split('.')[2] for k in dic_data]
Y = [v for v in dic_data.values()]
plt.plot(list(reversed(X)), Y)
plt.gca().invert_yaxis()
plt.show()