【问题标题】:Getting data through REST API and how to store it mysql database?通过 REST API 获取数据以及如何将其存储在 mysql 数据库中?
【发布时间】:2020-11-06 03:08:03
【问题描述】:

我刚刚通过 REST API 获取数据并想将其保存在 mysql 数据库中。由于我是初学者,只是寻找一种将数据保存到我的 sql 数据库中的方法。你们能否建议我一些教程/库/示例。那将是很大的帮助。谢谢。如果我反对 stackoverflow 的条款和条件,我真的很抱歉。但在这个话题上我真的需要帮助。

**from entsoe import EntsoePandasClient
import pandas as pd

client = EntsoePandasClient(api_key='api-key')

start = pd.Timestamp('20171201', tz='Europe/Brussels')
end = pd.Timestamp('20180101', tz='Europe/Brussels')
country_code = 'BE'  # Belgium

# methods that return Pandas Series
client.query_day_ahead_prices(country_code, start=start,end=end)
client.query_load(country_code, start=start,end=end)
client.query_load_forecast(country_code, start=start,end=end)
client.query_generation_forecast(country_code, start=start,end=end)

# methods that return Pandas DataFrames
client.query_wind_and_solar_forecast(country_code, start=start,end=end, psr_type=None)
client.query_generation(country_code, start=start,end=end, psr_type=None)
client.query_installed_generation_capacity(country_code, start=start,end=end, psr_type=None)
client.query_crossborder_flows('DE', 'DK', start=start,end=end)
client.query_imbalance_prices(country_code, start=start,end=end, psr_type=None)
client.query_unavailability_of_generation_units(country_code, start=start,end=end, docstatus=None)
#client.query_withdrawn_unavailability_of_generation_units('DE', start=start,end=end)

ts = client.query_day_ahead_prices(country_code, start=start, end=end)
print(ts)**

【问题讨论】:

    标签: python mysql pandas web-scraping rest


    【解决方案1】:

    使用 pandas 提供的to_sql 函数,您只需传递一个使用SQLAlchemy 库创建的连接对象:

    from sqlalchemy import create_engine
    engine = create_engine("mysql+pymysql://root:pass@localhost/mydb")
    
    df.to_sql('table_name_for_df', con=engine, if_exists='append', flavor='mysql')
    # check if the data has been sent
    engine.execute("SELECT * FROM table_name_for_df").fetchall()
    

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 2018-01-17
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多