Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.1 kB
1
Indexable
Never
import requests
import json
from sqlalchemy import create_engine, Column, Integer, String, Table, MetaData
from sqlalchemy.ext.declarative import declarative_base

url = 'https://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/en/une_rt_m?sinceTimePeriod=2017M01&precision=1&sex=T&unit=PC_ACT&s_adj=NSA&age=TOTAL'

response = requests.get(url)
data = json.loads(response.text)
engine = create_engine('sqlite:///mydatabase.db')
Base = declarative_base()

class EurostatData(Base):
    __tablename__ = 'eurostat_data'
    id = Column(Integer, primary_key=True)
    sex = Column(String)
    age = Column(String)
    unit = Column(String)
    s_adj = Column(String)
    value = Column(Integer)

Base.metadata.create_all(engine)
connection = engine.connect()


for item in data['value']:
    ins = EurostatData(sex=item.get('sex', ''), age=item.get('age', ''), unit=item.get('unit', ''), s_adj=item.get('s_adj', ''), value=item.get('value', ''))
    connection.execute(ins)


result = connection.execute("SELECT * FROM eurostat_data")
for row in result:
    print(row)