C# 独立可执行文件输出在C中为空
当我从命令行运行任何网站时,Python制作的独立可执行文件pwc.exe总是将网站html数据输出到任何网站的控制台 但是,当我尝试将输出读取为c字符串时,在大多数情况下,它只在非常小的网站上工作良好,我在c中得到一个空字符串 在这种情况下一切正常 命令行:pwc.exe gopro.com C参数行:arguments=gopro.com 控制台输出正确,但c字符串为空 命令行:pwc.exe www.bbc.com C参数行:arguments=www.google.com pwc.exe代码: 从lxml导入html 导入请求 导入系统 url=sys.argv[1] 主机=sys.argv[2] headers={'Host':主机,'用户代理':'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.geturl,headers=headers r、 编码='UTF-8' 打印r.text c代码:C# 独立可执行文件输出在C中为空,c#,python,web-crawler,C#,Python,Web Crawler,当我从命令行运行任何网站时,Python制作的独立可执行文件pwc.exe总是将网站html数据输出到任何网站的控制台 但是,当我尝试将输出读取为c字符串时,在大多数情况下,它只在非常小的网站上工作良好,我在c中得到一个空字符串 在这种情况下一切正常 命令行:pwc.exe gopro.com C参数行:arguments=gopro.com 控制台输出正确,但c字符串为空 命令行:pwc.exe www.bbc.com C参数行:arguments=www.google.com pwc.exe
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语言中正常工作
p、 美国试图这样读,但没用:
while (!proc.StandardOutput.EndOfStream)
{
html = proc.ou.ReadLine();
}
这是因为这些例外 您可以参考下面的代码来跟踪输出中的错误,可能您必须从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事件。