python - 从 C# 执行 python 文件时没有输出

标签 python c# visual-studio

我用 python 构建了一个 Webscraper,它在执行时生成正确的输出。现在我想将它实现到 C# 项目中,但它不起作用。我只是没有从中得到任何输出。我尝试在不同的步骤中找到将内容打印到标签的问题,但它似乎工作正常。我的 python 代码:

import bs4
import requests
from bs4 import BeautifulSoup

r = requests.get("https://fred.stlouisfed.org/series/AAA")
soup = bs4.BeautifulSoup(r.text,"lxml")
soup_find = soup.find_all("div",{"class":"pull-left meta-col"})[0].find("span",{"class":"series-meta-observation-value"}).text
print(soup_find)

还有我的 C# 代码(在 Visual Studio 中):

        private void cmd_scrape_Click(object sender, EventArgs e)
    {
        string fileName = @"My\path\to\Webscraper.py";
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(@"My\path\to\python.exe", fileName)
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        p.Start();
        lbl_Out.Text = "Starting...";
        string Output = p.StandardOutput.ReadToEnd();
        lbl_Out.Text = "Waiting for exit...";
        p.WaitForExit();
        lbl_Out.Text = "Done";
        lbl_Out.Text = Output;
    }

最佳答案

我无法直接回答你的问题,因为我没有安装 python,无法重现问题。但我将展示如何使用 C# 完全完成同样的事情。

我假设 C# 项目是 WinForms。

安装 2 个 NuGet 包(在 Visual Studio 菜单项目 -> 管理 Nuget 包中)

  • HtmlAgilityPack
  • Fizzler.Systems.HtmlAgilityPack

第一个提供 HTML 解析器,第二个提供扩展 HtmlNode.QuerySelectorQuerySelector 的查询语法几乎与 JavaScript 中的相同。

安装后将命名空间添加到代码中。

using HtmlAgilityPack;
using HtmlDocument = HtmlAgilityPack.HtmlDocument; // overrides class name conflict with System.Windows.Forms.HtmlDocument
using Fizzler.Systems.HtmlAgilityPack;

以及项目的全部代码

public partial class Form1 : Form
{
    private static readonly HttpClient client = new HttpClient();

    public Form1()
    {
        InitializeComponent();
    }

    private async void button1_Click(object sender, EventArgs e)
    {
        try
        {
            label1.Text = "Connecting";
            using (HttpResponseMessage response = await client.GetAsync("https://fred.stlouisfed.org/series/AAA", HttpCompletionOption.ResponseHeadersRead))
            {
                response.EnsureSuccessStatusCode();
                label1.Text = "Receiving data";
                string text = await response.Content.ReadAsStringAsync();
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(text);
                label1.Text = doc.DocumentNode.QuerySelector("div.pull-left.meta-col span.series-meta-observation-value").InnerText;
            }
        }
        catch (Exception ex)
        {
            label1.Text = "Error";
            MessageBox.Show(ex.Message + "\r\n\r\n" + ex.StackTrace);
        }
    }
}

enter image description here

关于python - 从 C# 执行 python 文件时没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62615247/

相关文章:

c# - 如何创建///摘要

Python-报告实验室 : Error using custom font

C#构造函数和构造对象的类型

c# - 为 ASP.NET 编写月份和年份下拉列表的最佳方法是什么?

c# - 如何在Linux服务器上轻松运行C#代码?

Asp.net 核心 + IIS Express。如何查看日志消息?

c++ - 错误 MSB6006 "CL.exe"在 vi​​sual studio c++ 中以代码 2 退出

Python tkinter 时间.sleep()

javascript - 如何将这个简单的 python 代码转换为 javascript?

python - Pygame -> 在类方法中移动一个矩形