Skip to content

Commit 9da0b4e

Browse files
committed
Append cbr_ru/xml_metall/main.py
1 parent ca7aaef commit 9da0b4e

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

cbr_ru/xml_metall/main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = 'ipetrash'
5+
6+
7+
if __name__ == '__main__':
8+
from datetime import date
9+
date_req1 = '01.01.2000'
10+
date_req2 = date.today().strftime('%d.%m.%Y')
11+
12+
file_name = 'metall_{}-{}.xml'.format(date_req1, date_req2)
13+
14+
# Кеширование. Если файла нет, то скачиваем его.
15+
import os.path
16+
if not os.path.exists(file_name):
17+
url = 'http://www.cbr.ru/scripts/xml_metall.asp?date_req1={}&date_req2={}'.format(date_req1, date_req2)
18+
19+
with open(file_name, 'w') as f:
20+
import requests
21+
rs = requests.get(url)
22+
f.write(rs.text)
23+
24+
with open(file_name, 'rb') as f:
25+
from bs4 import BeautifulSoup
26+
root = BeautifulSoup(f, "xml")
27+
28+
# Code="1" -- Золото
29+
# Code="2" -- Серебро
30+
# Code="3" -- Платина
31+
# Code="4" -- Палладий
32+
records = root.find_all('Record', attrs=dict(Code="1"))
33+
34+
# for i, record in enumerate((records[0], records[-1]), 1):
35+
for i, record in enumerate(records, 1):
36+
# b'<Record Date="06.01.2000" Code="1"><Buy>231,94</Buy><Sell>246,67</Sell></Record>\n\n'
37+
# record["Code"]
38+
print('{}. {}: {}'.format(i, record["Date"], record.findChild("Buy").text))

0 commit comments

Comments
 (0)