3

I build a Webscraper in python, which generates the right output when executed. Now I wanted to implement it to a C# project and it doesn´t work. I just don´t get any ouput from it. I tried finding a problem with printin things to labels in different steps but it seems to work fine. My python code:

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)

And my C# Code (in 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;
    }
3
  • Maybe this may help Commented Jun 27, 2020 at 20:57
  • Unfortunatly it didnt help, I tried to modify my code, but nothing Commented Jun 28, 2020 at 12:28
  • As an idea, try adding newline \r\n to python's output at the end of data. Commented Jun 28, 2020 at 14:19

2 Answers 2

2

I can't directly answer to your question because I have no python installed and can't reproduce the problem. But I'll show how to do the same thing completely with C#.

I assume that the C# project is WinForms.

Install 2 NuGet packages (in Visual Studio menu Project -> Manage Nuget Packages)

  • HtmlAgilityPack
  • Fizzler.Systems.HtmlAgilityPack

First one provides HTML parser, 2nd provides an extension HtmlNode.QuerySelector. Query syntax for QuerySelector is almost the same as in JavaScript.

After installation add namespaces to the code.

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

And whole code of the project

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the code, I hope you are fine with me using it for my application?
@Marvlogs You're welcome. Everything on StackOverflow is OK to use it for your application.
1

I tested the python and C# code, and it works fine and print "2.50". Maybe you can try to add break point to confirm whether the code in cmd_scrape_Click has been executed.

enter image description here

1 Comment

It has been executed, which I can see by removing the lbl_Out.Text=Output, because then it says "Done". There has to be something with the Output Text. I will keep trying, but thanks for the idea!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.