Flask:将requests.get API数据保存到sqlite数据库

时间:2018-08-19 17:31:45

标签: python flask sqlalchemy python-requests

我尝试将获取的API数据保存到Flask应用程序中的sqlite数据库中失败。我已经使用request.get()将外部API数据提取到数据框。 当我在Jupyter Notebook中对其进行测试时,功能“ extract_to_df_race”起作用。我放置了try-except语句以将错误消息打印到控制台。由于在控制台中没有记录任何错误消息,因此我最初假定数据已成功获取并保存到数据库。但是,在检查数据库时,没有任何记录被保存。

我使用了一个自定义的Flask命令来执行“ historical_records”功能,以一次性加载数据库。

有没有我可以尝试的更好的调试方法?

app / api / log.py

from app import app
from app.models import Race, db
from app.utils  import * 
import click

@app.cli.command()
def historical_records():
    seasons = [2015]
    races_round = range(1,5)
    df_races = extract_to_df_race('results', seasons, races_round)
    save_races_to_db(df_races, db)

def save_races_to_db(df_races, db):
    for idx,row in df_races.iterrows():
        r = Race()
        r.url = df_races.loc[idx,"url"]
        r.season = df_races.loc[idx,"season"]
        r.raceName = df_races.loc[idx,"raceName"]
        db.session.add(r)
        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            eprint(str(e))

要从虚拟环境中执行history_records功能,我先运行"export FLASK_APP=app/api/log.py",然后运行"flask historical_records"

app / utils.py

from __future__ import print_function
import requests
import json
import pandas as pd
import datetime
import sys

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

def extract_to_df_race(results_type, seasons, races_round):

    df_races = pd.DataFrame()

    if results_type == 'results':   
        for s in seasons:
            for r in races_round:
                try:
                    response = requests.get(API_URL)
                    response.raise_for_status()
                    dictionary = response.content 
                    dictionary = json.loads(dictionary)
                    races = transform_func(dictionary, s, r) 
                    df_races = pd.concat([df_races, races])
                except requests.exceptions.HTTPError as err:
                    eprint(err)
                    sys.exit(1)


        return df_races

竞赛模型

class Race(db.Model, Serializer):
    __tablename__ = 'races'
    raceId = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.String(50), unique=True)
    season = db.Column(db.Integer)
    raceName = db.Column(db.String(50))

    def __init__(self, **kwargs):
        super(Race, self).__init__(**kwargs)

0 个答案:

没有答案