【发布时间】:2019-04-15 16:52:20
【问题描述】:
当我从命令行运行它时,Python 制作的独立可执行文件 (pwc.exe) 总是将网站 html 数据输出到任何网站的控制台。
但是当我尝试将该输出读取到 c# 字符串时,在大多数情况下(它只在非常小的网站上运行良好)我在 c# 中得到一个空字符串。
-
在这种情况下一切正常
- 命令行:pwc.exe https://gopro.com/about-usgopro.com
- C# 参数行:Arguments = "https://gopro.com/about-usgopro.com"
-
控制台输出正确,但c#字符串为空
- 命令行:pwc.exe http://www.bbc.comwww.bbc.com
- C# 参数行:Arguments = "https://www.google.com www.google.com"
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