more G-Labs products

Author Topic: Best way to  (Read 3574 times)

November 17, 2014, 10:40:38 PM
Read 3574 times

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Hy there,

What is the best way to get values from a webpage?
1 - c# and parse it (how to do it?)
2 - c# to run shell script and get the values in json format and working them with c#
3 - python, but how to scrap the html?

Thanks

November 17, 2014, 11:18:23 PM
Reply #1

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I can't help with an example for you, but perhaps you could review the weather codes to see if they provide any useful information.

November 18, 2014, 08:46:47 AM
Reply #2

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
There are plenty of good articles on Screen Scraping in C#

If the above links don't help, could you give an example of the webpage value you would like to retrieve? I should be able to create some example code for you.


November 18, 2014, 04:12:40 PM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
How many of those components will work within HG?  I assume  you would need to use the NetHelper class and whatever methods Gene has included in it.  I've found that many times using code snippets won't work when they require outside classes since they may not be compiled into HG.

November 18, 2014, 07:16:16 PM
Reply #4

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
The WebClient is a standard .NET class, Gene probably makes use of it in his NetHelper Class.

Here is a simple example:
Code: [Select]
// declare the URL we want to scrape
string url = "http://www.homegenie.it/";
string webresult;

using (System.Net.WebClient client = new System.Net.WebClient())
{
  // populate webresult with the contents of the website
  webresult = client.DownloadString(url);
}

// Test. Count the number of characters in the string
Program.Notify("result length", webresult.Length.ToString());

The string 'webresult' contains the HTML content of the given website. At the end I just display a count of all the characters to see if something has been retrieved.

November 19, 2014, 09:54:21 PM
Reply #5

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
I get what i want in a mixed way...

Create a python scritp that i the run in hg using c#

But now i have another problem... how to pass the value to HG as watts values so i can record the watts and the see them in the analyse?

November 20, 2014, 06:13:27 AM
Reply #6

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
But now i have another problem... how to pass the value to HG as watts values so i can record the watts and the see them in the analyse?
Check the Energy Monitor code, a few lines out of that is what you need.

November 22, 2014, 09:33:07 PM
Reply #7

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
done that but never get values into analyse graphs...

November 22, 2014, 11:53:25 PM
Reply #8

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
you have to store the value in the "Meter.Watts" parameter:

Code: [Select]
Program.Parameter("Meter.Watts").Value = <value_read_from_sensor>;

stats will be shown after 5 minutes sampling.
Also read this:

http://www.homegenie.it/forum/index.php?topic=227.msg1201

g.


November 23, 2014, 11:34:17 PM
Reply #9

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Thank you.

Is woking now.

Gene using c# hg has any helper to work with json strings? What is the best way?

November 24, 2014, 12:47:38 AM
Reply #10

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Try this:

Code: [Select]
// example json input string
string jsonText = "{ \"Bar\" : \"something\" }";

// deserialize as dynamic
dynamic foo = JObject.Parse(jsonText);

// get the Bar field value
string bar = foo.Bar;

// output the result
Program.Notify("JSON Test", bar);

reference: http://thewayofcode.wordpress.com/2012/09/18/c-dynamic-object-and-json-serialization-with-json-net/

Have you considered using a Javascript app instead of a c# one? Perhaps it's easier to code.

g.
« Last Edit: November 24, 2014, 12:52:51 AM by Gene »

April 17, 2015, 10:23:13 AM
Reply #11

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
How can i make an javascript program to load a webpage and then serialize the html content?