[英]Scheduler makes a python script run but doesn't produce a csv file
I've written a script in python to scrape some links from a webpage and write them to a csv file. 我在python中编写了一个脚本来从网页上抓取一些链接并将它们写入csv文件。 My script does this in the right way when run it from an IDE.
从IDE运行时,我的脚本以正确的方式执行此操作。
When I run the same using windows task scheduler
, I can see that a command prompt
pops up and the script runs and prints result there as well but I don't get any csv file when the task is done. 当我使用
windows task scheduler
运行相同的操作时,我可以看到弹出一个command prompt
,脚本运行并在那里打印结果,但是在完成任务时我没有得到任何csv文件。
Am I missing something? 我错过了什么吗?
What possible change should I bring about to get a csv file when the script is run via .bat
file from windows task scheduler
? 当脚本通过
windows task scheduler
.bat
文件运行时,我应该采取哪些可能的更改来获取csv文件?
.bat
contains: .bat
包含:
@echo off
"C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\python.exe" "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\examplescript.py"
examplescript.py
contains: examplescript.py
包含:
import csv
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
url = "https://www.janglo.net/component/option,com_sobi2/"
def get_info(link):
res = requests.get(url)
soup = BeautifulSoup(res.text,"lxml")
for items in soup.select("#sobi2CatListSymbols .sobi2SubcatsListItems a[title]"):
if items.text=="Tutors":
ilink = f"{urljoin(url,items.get('href'))}"
return ilink
def get_links(tlink):
linklist = []
res = requests.get(tlink)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select(".sobi2ItemTitle a"):
linklist.append(urljoin(url,item.get("href")))
return linklist
if __name__ == '__main__':
with open("task_scheduler.csv","a",newline="") as infile:
writer = csv.writer(infile)
item = get_info(url)
for nlink in get_links(item):
print(nlink)
writer.writerow([nlink])
I've clearly specified the location of the ".bat" file below: 我已经清楚地指定了下面的“.bat”文件的位置:
"C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\test_schedule.bat"
Your code writes to the current working directory , because it opens a filename without a path. 您的代码写入当前工作目录 ,因为它打开没有路径的文件名。 The location of the
.bat
file does not determine the current working directory of code being run. .bat
文件的位置不确定正在运行的代码的当前工作目录 。
You didn't specify a working directory; 您没有指定工作目录; I'd expect that to be set with the Start in field in the scheduler, so the file is being created in the default location ,
%Windir%\\System32\\
. 我希望在调度程序中使用Start in字段进行设置,因此该文件是在默认位置
%Windir%\\System32\\
。
Either set a directory in the Start in field for the CSV file to be created in, have your batch script use cd
to move the current working directory to the right location, or use an absolute path when opening the file. 在“ 开始”字段中为要创建的CSV文件设置目录,让批处理脚本使用
cd
将当前工作目录移动到正确的位置,或在打开文件时使用绝对路径。
For example, if your batch script first runs cd /d %~dp0
then the working directory is changed to the directory of the batch script, and relative paths in Python are resolved against that location. 例如,如果批处理脚本首先运行
cd /d %~dp0
则工作目录将更改为批处理脚本的目录,并且Python中的相对路径将针对该位置进行解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.