• 0

C#]Get Data from Webpages ( + noob questions)


Question

Well im very new to programming i began from 0 with c# just a week or 2 ago

i just writed some little things on console and i changed to Forms 2 days ago, last one ( i spended like 10 hours trying to get it to work and searching info on System.IO lol :D ) is a litle Form with 3 textboxes that loads 3 currency values and the Date, it was writed from a file,you can modify it and then save the file again ( with 2 buttons load and save and a nice label that tells you the date... lol im happy of my tiny stupid application :p) v_v

( it works on on the tiny aplication but dont know still why i cant read the data on the file like a txt, it shows weird characters ( it is encrypted or something? i write the values as strings...)

well now what im trying to do now

Im trying to write to a textfile some Values of a webpage >_<

exactly what im trying is to get the "daily new currency values" from some webpage to write them on a file that the tiny program can read.

is anyway to tell the program something like

beetween that html code and that one is "x" currency, or from "here to here"

or some other way :?

thx

Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0

to do this, I normaly use WebClient(System.Web namespace) and RegularExpresion(System.Text.RegularExpressions namespace)

do a google query on these Classes.

if the text apears to be encoded it probalby is, perhaps its in UTF, or another text encoding.

if you can't get it to work,then I can post some code

Link to comment
Share on other sites

  • 0

i tryed to find info on those namespaces but well to tell the truth i dont undestand them the info seems to technically and not concrete :\

can i have a little example :?

Link to comment
Share on other sites

  • 0

namespace aTest
{
 ? ? ? ? ? ? ? ?using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Net;
	using System.Text;
	using System.Text.RegularExpressions;


	public class Test: System.Web.UI.Page
	{
 ?
 ?protected WebClient wc = new WebClient();

 ?private void Page_Load(object sender, System.EventArgs e)
 ?{
 ?	// Put user code to initialize the page here
 ?}

 ?#region Web Form Designer generated code
 ?override protected void OnInit(EventArgs e)
 ?{
 ?	//
 ?	// CODEGEN: This call is required by the ASP.NET Web Form Designer.
 ?	//
 ?	InitializeComponent();
 ?	base.OnInit(e);
 ?}
 ?
 ?/// &lt;summary&gt;
 ?/// ?Required method for Designer support - do not modify
 ?/// ?the contents of this method with the code editor.
 ?/// &lt;/summary&gt;
 ?private void InitializeComponent()
 ?{
 ?	
 ?	this.Load += new System.EventHandler(this.Page_Load);

 ?}
 ?#endregion

 ?private void getFromWeb(string uri)
 ?{
 ?
 ?	byte[] b;
 ?	b = wc.DownloadData(uri);
 ?	string downloaded = Encoding.ASCII.GetString(b);
 ?	Response.Write(downloaded);
 ?}
	}
}

this code if copy& pasted from one of my classes, there could be errors.

hint: there is a lot of good material concerning this on msdn( that includes the msdn library that comes along with VS.net), thats where I get most of what I need.

Link to comment
Share on other sites

  • 0

you can use both GET and POST;

eg. POST( from msdn)

try {
    
// Download the data to a buffer.
       WebClient client = new WebClient();

  Byte[] pageData = client.DownloadData("http://www.contoso.com");
string pageHtml = Encoding.ASCII.GetString(pageData);
Console.WriteLine(pageHtml);

// Download the data to a file.
        client.DownloadFile("http://www.contoso.com", "page.htm");

// Upload some form post values.
NameValueCollection form = new NameValueCollection();        
form.Add("MyName", "MyValue");        
Byte[] responseData = client.UploadValues("http://www.contoso.com/form.aspx", form);        

}
catch (WebException webEx) {
      Console.WriteLine(webEx.ToString());
       if(webEx.Status == WebExceptionStatus.ConnectFailure) {
           Console.WriteLine("Are you behind a firewall?  If so, go through the proxy server.");
       }
}

Link to comment
Share on other sites

  • 0

I asume that you didn't just copy and paste and asume it would work:

as you can see in my example there is no call to the getFromWeb() method.

you did pass the method a string containing the uri(including the http://...)?

Link to comment
Share on other sites

  • 0
you can use both GET and POST;

eg. POST( from msdn)

Do you know what the difference is between this and HttpRequests and HttpResponses? I was using this before, but this WebClient looks like it does the same thing.

Link to comment
Share on other sites

  • 0
I asume that you didn't just copy and paste and asume it would work
errr.... :whistle:
as you can see in my example there is no call to the getFromWeb() method.

uh.... :pinch: i can see it now :p

you did pass the method a string containing the uri(including the http://...)?

well.. ummm the uri is a variable that i need to put somewhere? like uri = http.... ?

to tell the the truth i didnt pass the "this program doesnt have a main entry"

Link to comment
Share on other sites

  • 0

umff >_<

im trying to do my best but i think this its beyond me :D

there are many things i dont understand on your code :\

can i have a bit extended code with that on a form that takes something from neowin for example?

sorry to annoy u :(

Link to comment
Share on other sites

  • 0

Ok, after some hours of headaches i got it to work :D thx :D

but now im having hard time trying to use Regular Expresions i cant find anything to filter only the Numbers from here:

align="right" class="yfnc_tabledata1">0.009124</td><td class="yfnc_tabledata1" align="right">1</td><td align="right" class="yfnc_tabledata1">0.007550</td><td align="right" class="yfnc_tabledata1">0.012006</td><td align="right" class="yfnc_tabledata1">0.005073</td><td align="right" class="yfnc_tabledata1">0.012999</td><td align="right" class="yfnc_tabledata1">0.011630</td></tr><tr><td class="yfnc_tablehead1"><table class="yfnc_tablehead1" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="left"><b> 1&nhref=/SIG=126noa14f/M=224039.2339938.3770379.2277546/D=fin/S=32797328:FOOT/EXP=1094021583/A=1226992/R=2/SIG=1136qnvkg/*/info/terms/>Terms of Service</a> - <a href=/SIG=126noa14f/M=224039.2339938.3770379.2277546/D=fin/S=32797328:FOOT/EXP=1094021583/A=1226992/R=3/SIG=11lp7krrc/*/info/copyright/copyright.html>Copyright Policy</a> - <a href="java script:ADFlaunch()">Ad Feedback</a></font></td></tr><tr><td align="center"></td></tr><tr><td align="center"><small>

Link to comment
Share on other sites

  • 0

A RegEx of "[0-9]*" would get you all of the numbers...

But im sure you need it narrowed down more specifically so if you give me a more specific example of where your numbers are i can help.

Also...

www.regexlib.com is a great RegEx source... Also it should have a download for The Regulator on there, get it, it will save you bullocks of time since you can test and debug RegEx in it.

Link to comment
Share on other sites

  • 0

the numbers i want are the ones are allways inside <> and almost always use a . like in

<td align="right" class="yfnc_tabledata1">0.005073</td>

just that there are like 12 of those in the page and the name of the tag is allways the same but diferent numbers

what im truing to do is to get the currency values from http://finance.yahoo.com/currency?u on the grid and use them on textboxes on a form as individuals....

Link to comment
Share on other sites

  • 0

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;

namespace regTest
{
	/// &lt;summary&gt;
	/// Summary description for test_regex.
	/// &lt;/summary&gt;
	public class test_regex : System.Web.UI.Page
	{
  protected WebClient wc = new WebClient();
  private void Page_Load(object sender, System.EventArgs e)
  {
 	 // Put user code to initialize the page here
 	 getAndSort("http://finance.yahoo.com/currency?u");
  }
  private void getAndSort(string uri)
  {
  
 	 byte[] b;
 	 
 	 Regex regex = new Regex("&lt;td align=\\\"right\\\" class=\\\"yfnc_tabledata1\\\"&gt;([0-9\\.]*)&lt;/td&gt;");
 	 
 	 b = wc.DownloadData(uri);
 	 string downloaded = Encoding.ASCII.GetString(b);
 	 //Response.Write(downloaded);
 	 MatchCollection match = regex.Matches(downloaded);
 	 
 	 string[] result = new string[match.Count];
 	 //Response.Write(match.Count);
 	 for(int i=0;i&lt; match.Count; i++)
 	 {

    result[i] = match[i].Groups[1].Value;
    Response.Write(result[i] +"\n");
 	 }

  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
 	 //
 	 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
 	 //
 	 InitializeComponent();
 	 base.OnInit(e);
  }
  
  /// &lt;summary&gt;
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// &lt;/summary&gt;
  private void InitializeComponent()
  {    
 	 this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
	}
}

this captures all the values minus the 1( that is dollar = dollar)

Link to comment
Share on other sites

  • 0
Well im very new to programming i began from 0 with c# just a week or 2 ago

i just writed some little things on console and i changed to Forms 2 days ago, last one ( i spended like 10 hours trying to get it to work and searching info on System.IO lol :D ) is a litle Form with 3 textboxes that loads 3 currency values and the Date, it was writed from a file,you can modify it and then save the file again ( with 2 buttons load and save and a nice label that tells you the date... lol im happy of my tiny stupid application :p) v_v

( it works on on the tiny aplication but dont know still why i cant read the data on the file like a txt, it shows weird characters ( it is encrypted or something? i write the values as strings...)

well now what im trying to do now

Im trying to write to a textfile some Values of a webpage >_<

exactly what im trying is to get the "daily new currency values" from some webpage to write them on a file that the tiny program can read.

is anyway to tell the program something like

beetween that html code and that one is "x" currency, or from "here to here"

or some other way :?

thx

Hi guy

same here, LOL :D , in this moment I work in C# attempt make screensaver (text mode) in VS.NET; when I use debug my stupid application works fine! then I try save this file with .scr extension instead .exe but I don't have sucess same in Run= screensaverbywinnerboy.scr.exe... only work in debug mode, do you have any idea I am noobie in C# lol.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.