如何在Azure上每24小时自动运行一次python脚本?

如何在Azure上每24小时自动运行一次python脚本?,python,azure,runbook,Python,Azure,Runbook,我有一个python脚本(用Jupyter笔记本编写),我想在Azure中运行这个脚本。python脚本基本上从API源(每24小时更新一次)获取数据,并更新Azure中的SQL数据库。因此,这个自动化的python脚本将在每次运行时更新数据库表 谁能帮我拿这个吗 下面是我编写的python代码 import pyodbc import requests import json import pandas as pd responses = requests.get("https://dat

我有一个python脚本(用Jupyter笔记本编写),我想在Azure中运行这个脚本。python脚本基本上从API源(每24小时更新一次)获取数据,并更新Azure中的SQL数据库。因此,这个自动化的python脚本将在每次运行时更新数据库表

谁能帮我拿这个吗

下面是我编写的python代码

import pyodbc
import requests
import json 
import pandas as pd

responses = requests.get("https://data.buffalony.gov/resource/d6g9-xbgu.json")

crime_data = json.loads(responses.text)

dic = {}

dic = crime_data

df = pd.DataFrame.from_dict(dic)

dff = df[['case_number','day_of_week','incident_datetime','incident_description','incident_id','incident_type_primary']].copy()

connection = pyodbc.connect ('Driver={ODBC Driver 17 for SQL Server};Server=servername;Database=Databasename;UID=admin;PWD=admin')

cur = connection.cursor()

row = []

for i in range(dff.shape[0]):

   row.append(dff.iloc[i].tolist())

sql = '''\
INSERT INTO [dbo].[FF] ([case_number],[day_of_week],[incident_datetime],[incident_description],[incident_id],[incident_type_primary]) values (?,?,?,?,?,?)
'''

for i in range(dff.shape[0]):

   cur.execute(sql,row[i])

connection.commit()

像Azure这样的任务调度器可以为您完成此任务。

我不使用Azure和jupyter笔记本,但我想我有一个解决方案 如果让计算机整夜运行,请将代码更改为:

import time
import pyodbc
import requests
import json 
import pandas as pd
while 1:
    responses = requests.get("https://data.buffalony.gov/resource/d6g9-xbgu.json")

    crime_data = json.loads(responses.text)

    dic = {}

    dic = crime_data

    df = pd.DataFrame.from_dict(dic)

    dff =  df    [['case_number','day_of_week','incident_datetime','incident_description','incident_i         d','incident_type_primary']].copy()

    connection = pyodbc.connect ('Driver={ODBC Driver 17 for SQL Server};Server=servername;Database=Databasename;UID=admin;PWD=admin')

    cur = connection.cursor()

    row = []

    for i in range(dff.shape[0]):
        row.append(dff.iloc[i].tolist())

    sql = '''\
    INSERT INTO [dbo].[FF] ([case_number],[day_of_week],[incident_datetime],    [incident_description],[incident_id],[incident_type_primary]) values (?,?,?,?,?,?)
    '''

    for i in range(dff.shape[0]):

        cur.execute(sql,row[i])

    connection.commit()
    time.sleep(86400)
如果没有,请在启动文件中创建一个新的python程序,如下所示:

import time, os
while 1:
    if time.ctime()[11:13] >= "update hour" and time.ctime()[0:4] != open("path/to/any_file.txt").read():
        file = open("path/to/any_file.txt", "w")
        file.write(time.ctime()[0:4])
        file.close()
        os.system("python /path/to/file.py")

谢谢我尝试了这种方法,但当我运行webjob时,我可以看到状态“失败”。我的代码是有效的,它运行良好的jupyter笔记本。这可能是什么原因?我是否应该像pandas一样安装库,因为我已经在脚本中包含了相同的库。如果是,我应该在哪里安装这些python库检查日志以了解失败的原因。最有可能涉及到缺少的Python库。下面的文章将帮助您了解如何安装这些库!