python - 使用任务计划程序自动执行 Python 脚本抓取
问题描述
我每天都在尝试运行我的 Python 脚本,我需要一种方法来定期自动运行我的 Python 脚本。首先我正在尝试制作bat文件,但它总是失败..我不知道为什么。仅供参考,我有 2 个 python exe,它让我在选择我应该使用什么时感到困惑,所以我在两个地方尝试但都失败了。
因为它失败了,我尝试了另一种填充参数并像这样开始的方式,但它也失败了,当我尝试运行任务时,它总是显示命令提示符并立即关闭但没有做任何事情,我我也尝试使用 ps1,但它是一样的。
我试图像 alperindo 所说的那样执行命令python emas_.py但它的 stil 给了我错误
这是我的代码python
import pandas as pd
import requests
from bs4 import BeautifulSoup
url = "https://www.indexmundi.com/commodities/?commodity=gold&months=300"
r = requests.get(url)
html = r.text
soup = BeautifulSoup(html)
table = soup.find('table', {"class": "tblData"})
rows = table.find_all('tr')
data = []
for row in rows[1:]:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
result = pd.DataFrame(data, columns=['month', 'price', 'change'])
result['month'] = pd.to_datetime(result["month"])
result.to_csv("emas_.csv", index=False)
df = pd.read_csv("emas_.csv")
pd.set_option('display.max_rows', df.shape[0]+1)
print(df)
import pyodbc
from sqlalchemy import create_engine
server = 'MSHULHAN\SQLEXPRESS'
database = 'daming'
engine = create_engine('mssql+pyodbc://' + server + '/' + database + '?trusted_connection=yes&driver=ODBC+Driver+13+for+SQL+Server')
#engine = create_engine('mysql://root:@localhost/daming') # enter your password and database names here
col_names = ["month", "price", "change"]
df = pd.read_csv("emas_.csv",sep=',',quotechar='\'',encoding='utf8', names=col_names,skiprows = 1) # Replace Excel_file_name with your excel sheet name
df.to_sql('emas',con=engine,index=False,if_exists='replace') # Replace Table_name with your sql table name
解决方案
我为此使用 NSSM:https ://nssm.cc/
它会将您的 Python 脚本安装为 Windows 服务。只需确保在Application
选项卡中,在字段中输入 Python 的路径,并在Path
字段中输入 Python 脚本Arguments
。如果脚本没有立即启动,请在选项卡的Error
andOutput
字段中输入有效路径以便能够进行调试。I/O
推荐阅读
- flutter - 列子容器覆盖父容器的全宽
- windows - 查找是否在带有 powershell 的 Windows 10 桌面中安装了包含 20 个应用程序的文本文件中的列表
- c# - UWP 根据单击的按钮更改 SplitView.Pane 内容
- flask - 我的 Heroku Flask Web 应用程序如何支持 N 个用户同时下载图像文件?
- python - Python setup 在他自己的目录中运行脚本
- python - 关于python中的“平等”
- html - div中自定义形状的颜色段
- jenkins - Jenkins - 构建开始时,中止同一分支的所有先前构建
- javascript - 保留jquery附加的输入
- xml - 操作其中包含数组的 XML