无法在 运行 python scrapy spider 的 c# 中编写脚本

Unable to run python script in c# which runs scrapy spider

我关注了 this_link 并且我能够从我的 c# 代码中 运行 一个虚拟的 python 文件...

   public JsonResult FetchscrapyDataUrl(String website)
        {

           ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = @"C:\ProgramData\Anaconda3\python.exe";            
            start.Arguments = @"C:\Users\PycharmProjects\scraping_web\scrape_info\main.py";
           //this is path to .py file from scrapy project

            start.CreateNoWindow = false;  // We don't need new window
            start.UseShellExecute = false;  // Do not use OS shell
            //start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
            start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
            Console.WriteLine("Python Starting");
       

            start.RedirectStandardOutput = true;
            using (Process process = Process.Start(start))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
                    string result = reader.ReadToEnd();  // Here is the result of StdOut(for example: print "test")
                    Console.Write(result);
                }
            }
    
    }
       

现在我知道我可以 运行 scrapy 像这样从单个文件 main.py 抓取...

from scrapy import cmdline    
cmdline.execute("scrapy crawl text".split())

当我在 windows 中从 cmd 运行 main.py 文件时它工作正常,但当我从 C# 代码 .Net 框架 运行 它时它不起作用。错误是...

"Scrapy 1.4.0 - no active project\r\n\r\nUnknown command: crawl\r\n\r\nUse \"scrapy\" to see available commands\r\n"

知道如何 运行 这个...或者我是否缺少 windows 中的一些路径设置??

或者我应该 运行 我的蜘蛛从 C# 以其他方式??

您需要设置 WorkingDirectory 属性

start.WorkingDirectory = @"C:\Users\PycharmProjects\scraping_web\scrape_info\"

或者您需要 cd 到该目录才能使其工作