【问题标题】:Standalone executable output is empty in C#独立的可执行输出在 C# 中为空
【发布时间】:2019-04-15 16:52:20
【问题描述】:

当我从命令行运行它时,Python 制作的独立可执行文件 (pwc.exe) 总是将网站 html 数据输出到任何网站的控制台。

但是当我尝试将该输出读取到 c# 字符串时,在大多数情况下(它只在非常小的网站上运行良好)我在 c# 中得到一个空字符串。

  1. 在这种情况下一切正常

  2. 控制台输出正确,但c#字符串为空

pwc.exe 代码:

from lxml import html import requests import sys url=sys.argv[1] host=sys.argv[2] headers = {'Host': host, 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0', 'Accept' : 'Accept: text/css,*/*;q=0.1', 'Accept-Language':'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate, br', 'Connection':'keep-alive'} r = requests.get(url, headers = headers) r.encoding = 'UTF-8' print (r.text)

c#代码:

        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = AppDomain.CurrentDomain.BaseDirectory + @"pwc.exe",
                Arguments = "https://www.bbc.com/about-us www.bbc.com",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden
            }
        };

        proc.Start();
        string html = proc.StandardOutput.ReadToEnd();

我需要将 pwc.exe 控制台输出 (utf8) 转换为 C# 字符串。看起来当我读取非常小的页面的输出时,在 C# 中一切正常。

附言尝试这样阅读,但没有帮助:

while (!proc.StandardOutput.EndOfStream)
{
html = proc.ou.ReadLine();
}

【问题讨论】:

    标签: c# python web-crawler


    【解决方案1】:

    正是因为这些例外。

    您可以参考下面的代码来跟踪输出中的错误,可能您必须从 python 端进行一些转换才能在 C# 代码中正确接收。

    private static void ProcessItem()
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = AppDomain.CurrentDomain.BaseDirectory + @"dist\Webpy\webpy.exe",
                    //Arguments = "https://gopro.com/about-us gopro.com",
                    //Arguments = "https://www.google.com www.google.com",
                    Arguments = "https://www.bbc.com/about-us www.bbc.com",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                }
            };
            //* Set your output and error (asynchronous) handlers
            process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
            process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
            //* Start process and handlers
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit();
        }
    
        static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            //* Do your stuff with the output (write to console/log/StringBuilder)
            Console.WriteLine(outLine.Data);
        }
    

    【讨论】:

    • 这没有帮助。最后,我使用 python 写入文件(导入编解码器以编写 utf8),然后使用 c# 读取该文件,它没有任何问题。
    • 感谢您告知我们的方法。我编写上面的代码是为了更好地调试而不是作为解决方案的一部分。
    • 我不知道为什么,但是在这两种情况下都没有捕获到那些 OutputDataReceived 事件。
    猜你喜欢
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多