-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_data.py
More file actions
47 lines (42 loc) · 1.57 KB
/
load_data.py
File metadata and controls
47 lines (42 loc) · 1.57 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests
from bs4 import BeautifulSoup
from .models import Wallet_value, Wallet_indentificator
def load_data(date_):
req = requests.get("http://www.cbr.ru/scripts/XML_daily.asp?date_req=" +
date_)
soup = BeautifulSoup(req.content, 'lxml')
if date_.split('/') == str(soup.find('valcurs')['date']).split('.'):
ids = soup.find_all('valute')
for i in ids:
try:
wallet = Wallet_indentificator.objects.get(
wallet_name=i.find('name').text
)
except:
wallet = Wallet_indentificator(
wallet_name=i.find('name').text,
wallet_id=i['id'],
wallet_char_code=i.find('charcode').text,
)
wallet.save()
data = Wallet_value(
wallet=wallet,
wallet_nominal=int(i.find('nominal').text),
wallet_value=float(".".join(i.find('value').text.split(','))),
date='-'.join(soup.find('valcurs')['date'].split('.')[::-1])
)
data.save()
else:
raise ValueError
def load_name():
req = requests.get("http://www.cbr.ru/scripts/XML_daily.asp?date_req=03"
"/03/2020")
soup = BeautifulSoup(req.content, 'lxml')
ids = soup.find_all('valute')
for i in ids:
a = Wallet_indentificator(
wallet_name=i.find('name').text,
wallet_id=i['id'],
wallet_char_code=i.find('charcode').text,
)
a.save()