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;
}
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)
HtmlAgilityPackFizzler.Systems.HtmlAgilityPackFirst 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);
}
}
}

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With