首页 > 解决方案 > 使用任务计划程序自动执行 Python 脚本抓取

问题描述

我每天都在尝试运行我的 Python 脚本,我需要一种方法来定期自动运行我的 Python 脚本。首先我正在尝试制作bat文件,但它总是失败..我不知道为什么。仅供参考,我有 2 个 python exe,它让我在选择我应该使用什么时感到困惑,所以我在两个地方尝试但都失败了。 蝙蝠 1

在此处输入图像描述

因为它失败了,我尝试了另一种填充参数并像这样开始的方式,但它也失败了,当我尝试运行任务时,它总是显示命令提示符并立即关闭但没有做任何事情,我我也尝试使用 ps1,但它是一样的。

错误 3

我试图像 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

标签: pythonanacondajupyterscreen-scraping

解决方案


我为此使用 NSSM:https ://nssm.cc/

它会将您的 Python 脚本安装为 Windows 服务。只需确保在Application选项卡中,在字段中输入 Python 的路径,并在Path字段中输入 Python 脚本Arguments。如果脚本没有立即启动,请在选项卡的ErrorandOutput字段中输入有效路径以便能够进行调试。I/O


推荐阅读