自动执行 python 脚本每天运行两次
我在尝试自动化我的 python 脚本以在每天上午 9 点和中午自动获取我们的电子商店订单时遇到问题。当在我的环境中运行时以及作为我将脚本转换为的可执行文件运行时(即我双击以像应用程序一样运行它),该程序按预期工作。
当我尝试使用 Windows 任务计划程序自动执行此过程时,出现了我的问题。在大约一周的时间里,我让这个过程无缝运行,因为我让程序在连接到服务器的虚拟机上运行,并且它始终处于开启状态,并且每天早上 9 点和中午都会运行脚本。现在,我遇到超时问题“httpsconnectionpool(host='https' port=443)”和ImportError:无法从部分初始化的模块“pip._vendor”导入名称“例外”。 urllib3'(很可能是由于循环导入) 作为一个错误,如果有人可以推荐或指出我的错误的潜在修复方法,我愿意采用其他方法来自动化此操作。 进步。
import csv
from datetime import datetime, timedelta, date
import os
from woocommerce import API
import urllib3
wcapi = API(
url="My_URL",
consumer_key="my_key", # Your consumer key
consumer_secret="My_secret", # Your consumer secret
wp_api=True, # Enable the WP REST API integration
version="wc/v3")
yesterday = datetime.now() - timedelta(hours=24)
today = date.today()
time = datetime.now()
current_time = time.strftime("%H:%M:%S")
file_date = datetime.now().strftime("%m-%d-%Y-%H%M")
r = wcapi.get(f"orders?after={yesterday}")
meta = []
if r.status_code in range(200, 299):
orders = r.json()
# set the path that you want to save the file
dir_path = "\Online_Orders\Estore Orders\"
# set the name of the file with todays date
file_name = f"orders-{file_date}.csv"
# create the full file path
file_path = os.path.join(dir_path, file_name)
with open(file_path, 'w', newline='', encoding='utf_8') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
# delcare all of the column headers in the csv file
header = ['id', 'status', 'date_created', 'total', 'shipping_total', 'total_tax', 'sku', 'item_name',
'item_quantity', 'item_price', 'item_total',
'first_name', 'last_name', 'company', 'email', 'phone',
'billing_address1', 'billing_address2', 'billing_city', 'billing_state',
'billing_postcode', 'billing_country',
'shipping_address1', 'shipping_address2', 'shipping_city',
'shipping_state', 'shipping_postcode', 'shipping_country', 'customer_note',
'shipping_method_title', 'shipping_method_id', 'payment_method', 'payment_method_title', 'meta']
# write headers to csv file
writer.writerow(header)
for order in orders:
# create dictionary reference variables for nested dictionaries to clean up keys below
shipping = order['shipping']
billing = order['billing']
shipping_lines = order['shipping_lines']
# this for look goes through the list of shipping_line items for shipping information and matches the pairs
# in dictionary format to be accessed below
for ship_info in shipping_lines:
pass
for line_item in order['line_items']:
# create the new row of data to send to csv
row = []
# create a dictionary out of a list of meta data and assign the meta data list to the meta var
mt = line_item['meta_data']
for m in mt:
if m['key'] == 'vpc-cart-data':
meta = m
# order information
row.append(order['id'])
row.append(order['status'])
row.append(order['date_created'])
row.append(order['total'])
row.append(order['shipping_total'])
row.append(order['total_tax'])
row.append(line_item['sku'])
row.append(line_item['name'])
row.append(line_item['quantity'])
row.append(line_item['price'])
row.append(line_item['total'])
# customer information
row.append(billing['first_name'])
row.append(billing['last_name'])
row.append(billing['company'])
row.append(billing['email'])
row.append(billing['phone'])
# billing information
row.append(billing['address_1'])
row.append(billing['address_2'])
row.append(billing['city'])
row.append(billing['state'])
row.append(billing['postcode'])
row.append(billing['country'])
# shipping information
row.append(shipping['address_1'])
row.append(shipping['address_2'])
row.append(shipping['city'])
row.append(shipping['state'])
row.append(shipping['postcode'])
row.append(shipping['country'])
row.append(order['customer_note'])
row.append(ship_info['method_title'])
row.append(ship_info['method_id'])
# payment information
row.append(order['payment_method'])
row.append(order['payment_method_title'])
# meta
row.append(meta)
# write each row to the csv file
writer.writerow(row)
# clear accumulated meta data after row is written
meta.clear()
I am having issues attempting to automate my python script to automatically get our estore orders at 9am and noon each day. The program works as expected when ran through my environment and when being run as an executable file that I converted the script to(i.e. me double clicking to run it like an application).
My problem occurs when I try to automate this process with windows task scheduler. For about a week I had the process working seamlessly as I had the program running on our VM connected to our server and it was always on and would run the script everyday at 9am and noon. Now I am getting Timeout issues "httpsconnectionpool(host='https' port=443)' and ImportError: cannot import name 'exceptions' from partially initialized module 'pip._vendor.urllib3' (most likely due to a circular import) as an error. I am open to other methods to automate this if someone could recommend that or point out the potential fix for my error. Thank you in advance.
import csv
from datetime import datetime, timedelta, date
import os
from woocommerce import API
import urllib3
wcapi = API(
url="My_URL",
consumer_key="my_key", # Your consumer key
consumer_secret="My_secret", # Your consumer secret
wp_api=True, # Enable the WP REST API integration
version="wc/v3")
yesterday = datetime.now() - timedelta(hours=24)
today = date.today()
time = datetime.now()
current_time = time.strftime("%H:%M:%S")
file_date = datetime.now().strftime("%m-%d-%Y-%H%M")
r = wcapi.get(f"orders?after={yesterday}")
meta = []
if r.status_code in range(200, 299):
orders = r.json()
# set the path that you want to save the file
dir_path = "\Online_Orders\Estore Orders\"
# set the name of the file with todays date
file_name = f"orders-{file_date}.csv"
# create the full file path
file_path = os.path.join(dir_path, file_name)
with open(file_path, 'w', newline='', encoding='utf_8') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
# delcare all of the column headers in the csv file
header = ['id', 'status', 'date_created', 'total', 'shipping_total', 'total_tax', 'sku', 'item_name',
'item_quantity', 'item_price', 'item_total',
'first_name', 'last_name', 'company', 'email', 'phone',
'billing_address1', 'billing_address2', 'billing_city', 'billing_state',
'billing_postcode', 'billing_country',
'shipping_address1', 'shipping_address2', 'shipping_city',
'shipping_state', 'shipping_postcode', 'shipping_country', 'customer_note',
'shipping_method_title', 'shipping_method_id', 'payment_method', 'payment_method_title', 'meta']
# write headers to csv file
writer.writerow(header)
for order in orders:
# create dictionary reference variables for nested dictionaries to clean up keys below
shipping = order['shipping']
billing = order['billing']
shipping_lines = order['shipping_lines']
# this for look goes through the list of shipping_line items for shipping information and matches the pairs
# in dictionary format to be accessed below
for ship_info in shipping_lines:
pass
for line_item in order['line_items']:
# create the new row of data to send to csv
row = []
# create a dictionary out of a list of meta data and assign the meta data list to the meta var
mt = line_item['meta_data']
for m in mt:
if m['key'] == 'vpc-cart-data':
meta = m
# order information
row.append(order['id'])
row.append(order['status'])
row.append(order['date_created'])
row.append(order['total'])
row.append(order['shipping_total'])
row.append(order['total_tax'])
row.append(line_item['sku'])
row.append(line_item['name'])
row.append(line_item['quantity'])
row.append(line_item['price'])
row.append(line_item['total'])
# customer information
row.append(billing['first_name'])
row.append(billing['last_name'])
row.append(billing['company'])
row.append(billing['email'])
row.append(billing['phone'])
# billing information
row.append(billing['address_1'])
row.append(billing['address_2'])
row.append(billing['city'])
row.append(billing['state'])
row.append(billing['postcode'])
row.append(billing['country'])
# shipping information
row.append(shipping['address_1'])
row.append(shipping['address_2'])
row.append(shipping['city'])
row.append(shipping['state'])
row.append(shipping['postcode'])
row.append(shipping['country'])
row.append(order['customer_note'])
row.append(ship_info['method_title'])
row.append(ship_info['method_id'])
# payment information
row.append(order['payment_method'])
row.append(order['payment_method_title'])
# meta
row.append(meta)
# write each row to the csv file
writer.writerow(row)
# clear accumulated meta data after row is written
meta.clear()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论