-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
31 lines (25 loc) · 1.14 KB
/
main.py
File metadata and controls
31 lines (25 loc) · 1.14 KB
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
import json
import re
import requests
import csv
URL = " https://www.cbr-xml-daily.ru/archive/2022/10/22/daily_json.js"
HEADER = ["Day", "Exchange rate"]
write_to_file = "C:/Users/esh20/Desktop/dataset.csv"
with open(write_to_file, "w") as file:
dw = csv.DictWriter(file, delimiter=',',
fieldnames=HEADER)
dw.writeheader()
while True:
html_text = requests.get(URL).text
json_pars = json.loads(html_text)
current_day = json_pars["Date"]
final_current_day = re.findall(r"(\d{4}\-\d{2}\-\d{2})", current_day)
current_course = json_pars["Valute"]["USD"]["Value"]
previous_day_url = json_pars["PreviousURL"]
final_previous_day_url = previous_day_url.replace("//", "")
switch_previous_day_url = "http://" + final_previous_day_url
finish_current_day = str(final_current_day)[2:-2]
finish_current_day = finish_current_day.replace("'", "")
with open(write_to_file, "a", newline="") as file:
writer = csv.DictWriter(file, fieldnames=HEADER)
writer.writerow({"Day": finish_current_day, "Exchange rate": current_course})