简体   繁体   English

Scheduler使python脚本运行但不生成csv文件

[英]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.

相关问题 批处理文件中的Python脚本无法在任务计划程序中运行 - Python script from batch file won't run in task scheduler Python脚本创建文件但不写入任何内容 - Python script makes a file but doesn't write anything Python调度程序不运行工作功能 - Python scheduler doesn't run working function Python 脚本不会从 .bat 文件、任务计划程序或 cmd 提示符运行 - Python script won't run from .bat file, Task Scheduler, or cmd prompt 作为systemd服务运行时,Python脚本不会写入文件 - Python script doesn't write to file when run as a systemd service PYTHON 和批处理脚本:如果文件存在则运行文件,如果不存在则创建 - PYTHON AND BATCH SCRIPT: Run file if it exists and create if it doesn't python脚本在调度程序下无法正常工作 - python script doesn't work properly under scheduler 通过任务计划程序运行时,Python脚本未创建文件 - Python Script not creating file when run through Task Scheduler 我该怎么做才能使python脚本可以从任何目录运行:脚本文件不必与.csv文件位于同一目录中? - What do I do to make a python script that can run from any directory: the script file doesn’t have to be in the same directory as the .csv files? 使用python make进行写入和写入文件,但不写入或关闭文件 - Make and write to file in python makes but doesn't write or close file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM