-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
51 lines (44 loc) · 1.53 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
47
48
49
50
51
import os
import cloudscraper
import zipfile
import codecs
import pandas as pd
from bs4 import BeautifulSoup
outdir = "output/"
s = cloudscraper.create_scraper(disableCloudflareV1=True, browser="chrome", delay=10)
for _ in range(3):
r = s.get("https://cbr.ru/s/newbik", allow_redirects=True)
if r.status_code != 200:
print(f"Error downloading zip: {r.status_code}")
continue
open("latest.zip", "wb").write(r.content)
break
try:
with zipfile.ZipFile("latest.zip", "r") as zr:
zr.extractall(outdir)
os.remove("latest.zip")
except Exception as e:
print(f"Error unpacking file: {e}")
exit(1)
for fp in os.listdir(outdir):
if not fp.endswith("_full.xml"):
continue
with codecs.open(outdir + fp, "r", encoding="windows-1251") as f:
content = f.read()
s = BeautifulSoup(content, "lxml")
data = []
for entry in s.findAll("bicdirectoryentry"):
row = entry.attrs
for participant_info in entry.findAll("participantinfo"):
row.update(participant_info.attrs)
for swbic in entry.findAll("swbics"):
row.update(swbic.attrs)
for account in entry.findAll("accounts"):
row.update(account.attrs)
data.append(row)
df = pd.DataFrame(data)
df.to_csv(outdir + "latest.csv", index=False, header=True)
df.to_csv(outdir + "latest.tsv", index=False, header=True, sep="\t", quoting=3)
df.to_json(outdir + "latest.json", orient="records")
df.to_xml(outdir + "latest.xml")
os.remove(outdir + fp)