Files
Excel-project/server/backend/api/companies.py
2026-01-06 18:06:34 +03:00

42 lines
1.4 KiB
Python

import requests
import xml.etree.ElementTree as ET
from base64 import b64encode
from server.backend.schemas.pydantic import settings
import pandas as pd
from server.backend.api.session import get_session
session = get_session({
"Accept": "application/xml",
})
def fetch_contragents():
response = session.get(settings.URL_COMPANIES, timeout=10)
response.raise_for_status()
return response.text
def parse_contragents(xml: str):
try:
NS = {
"atom": "http://www.w3.org/2005/Atom",
"m": "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
"d": "http://schemas.microsoft.com/ado/2007/08/dataservices",
}
rows = []
# Парсинг XML-ответа
root = ET.fromstring(xml)
# Извлечение конкретных данных
for entry in root.findall('atom:entry', NS):
properties = entry.find('atom:content',NS).find(
'm:properties', NS)
rows.append({
'Ref_Key': properties.findtext('d:Ref_Key', default=None, namespaces=NS),
'Description': properties.findtext('d:Description', default=None, namespaces=NS),
})
df = pd.DataFrame(rows)
return df
except ET.ParseError:
raise
def companies():
xml_data = fetch_contragents()
root = parse_contragents(xml_data)
root.to_excel("./excel_files/companies.xlsx")