简体   繁体   English

使用 Task Scheduler 自动抓取 Python 脚本

[英]Automate Python Scripts Scraping with Task Scheduler

I'm trying to running my Python Scripts every day , I need a way to run my Python Scripts periodically and automatically.我每天都在尝试运行我的 Python 脚本,我需要一种方法来定期自动运行我的 Python 脚本。 first I'm trying to make bat file but it always failed.. I don't know why.首先我正在尝试制作 bat 文件,但它总是失败..我不知道为什么。 FYI I got 2 python exe and its makes me confuse to choose what should I use, so I try in two place but both failed.仅供参考,我有 2 个 python exe,它让我无法选择应该使用什么,所以我在两个地方尝试,但都失败了。 蝙蝠 1

在此处输入图片说明

because its failed, I tried another way which fills the arguments and starts like this, but it's failed too, when I'm trying to run the task, it's just always show command prompt and immediately closed but didn't do anything, I'm trying with ps1 too but its the same.因为它失败了,我尝试了另一种填充参数并像这样开始的方法,但它也失败了,当我尝试运行任务时,它总是显示命令提示符并立即关闭但没有做任何事情,我我也尝试使用 ps1 但它是一样的。

错误 3

i tried to execute the command python emas_.py like alperindo said but its stil gives me error我试图像 alperindo 所说的那样执行命令python emas_.py但它的 stil 给了我错误命令错误

here's my code in python这是我在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

I'm using NSSM for that: https://nssm.cc/我为此使用 NSSM: https ://nssm.cc/

It will install your Python script as a Windows service.它会将您的 Python 脚本安装为 Windows 服务。 Just make sure that in the Application tab, you enter the path to your Python in the Path field and your Python script in the Arguments field.只需确保在“ Application选项卡中的“ Path字段中输入 Python 的Path并在“ Arguments字段中输入 Python 脚本。 Enter valid paths in the Error and Output field in the I/O tab to be able to debug if the script doesn't start right away.I/O选项卡的ErrorOutput字段中输入有效路径,以便在脚本没有立即启动时进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM