Pobieranie danych live

0

Ściągaj kod strony i go rozbieraj wczytując interesujące cię informacje.

Uzyj do tego HTML DOM

0

Znalazłem coś takiego:

private void processHTML(String htmlContent)
{
// Obtain the document interface
IHTMLDocument2 htmlDocument = (IHTMLDocument2)new mshtml.HTMLDocument();

        // Construct the document
        htmlDocument.write(htmlContent);

        listBox1.Items.Clear();
                  
        // Extract all elements
        
        IHTMLElementCollection allElements = htmlDocument.all;

        // Iterate all the elements and display tag names
        foreach (IHTMLElement element in allElements)
        {
            listBox1.Items.Add(element.tagName);
        }
        

    }

Niestety nie rozumiem jak mam teraz wyłuskać z tej kolekcji elementów , wartość komórki z tabelki a zadanym id. Czytałem się służą do tego metody GetElementById niestety nie potrafię jej użyć. Mogę prosić o pomoc albo jakiś prosty przykład użycia?

0

http://htmlagilitypack.codeplex.com/ -> Musisz dodać tą dll'ke (Solution explorer -> Add Reference->Broswer->HtmlAgilityPack.dll)

Zastanów się czy nie lepiej ściągać te dane z Yahoo!/finance.google.com. Tu masz przykład PHP`ie: http://213.227.70.223/public/php_code/YahooQuotes/quotes_update.phps

 
using System;
using System.Collections.Generic;
using System.Text;
using HtmlAgilityPack;
using System.Net;
using System.IO;
using System.Globalization;

/* sztuczne.konto_at_gmail_dot_com */
namespace Forex
{
  public class FXStreetQuote
    {
        public string Name { get; set; }
        public decimal Last { get; set; }
        public decimal Open { get; set; }
        public decimal High { get; set; }
        public decimal Low { get; set; }
        public decimal NetChg { get; set; }
        public decimal PerChg { get; set; }
      
        
       public FXStreetQuote(string name, decimal last, decimal open, decimal high, decimal low, decimal netchg, decimal perchg)
       {
           this.Name = name;
           this.Last = last;
           this.Open = open;
           this.High = high;
           this.Low = low;
           this.NetChg = netchg;
           this.PerChg = perchg;

       }
  }


     public class FXStreet
        {
         public Uri url;
         protected string _glue = "%3b";
         protected string _url = @"http://www.fxstreet.com/rates-charts/currency-rates/?id=majors%3b";
         protected CultureInfo ci_provider = CultureInfo.InvariantCulture;

         public FXStreet(List<string> currencies)
         {
             if(currencies.Count < 1)
             {
                 throw new Exception("error");

             }
             
             StringBuilder sb = new StringBuilder(_url);
             foreach(string item in currencies)
             {
                
                 sb.Append(item.ToLower());
                 sb.Append(_glue);
             }

           sb.Remove(sb.Length - 3, 3);
           url = new Uri(sb.ToString(), UriKind.Absolute);
         
        }

         public List<FXStreetQuote> Download()
         {

             List<FXStreetQuote> list = new List<FXStreetQuote>();

             WebClient wb = new WebClient();
             HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            
             html.Load( new MemoryStream(wb.DownloadData(url)));
             
             HtmlNodeCollection currencies_nodes = html.DocumentNode.SelectNodes("//div[@class='table-ttpushrates table-rates it-TTPushRates']/table/tbody/tr");

             if (currencies_nodes != null)
             {
                 foreach (HtmlNode tr_node in currencies_nodes)
                 {
                     HtmlNodeCollection tds =  tr_node.SelectNodes("td");
                     if (tds != null)
                     {
                         decimal last, open, high, low, change, perchg = 0;

                         string currency = tds[0].InnerText;
                         
                         decimal.TryParse(tds[1].InnerText, NumberStyles.Any, ci_provider, out last);
                         decimal.TryParse(tds[2].InnerText, NumberStyles.Any, ci_provider, out open);
                         decimal.TryParse(tds[3].InnerText, NumberStyles.Any, ci_provider, out high);
                         decimal.TryParse(tds[4].InnerText, NumberStyles.Any, ci_provider, out low);
                         decimal.TryParse(tds[5].InnerText, NumberStyles.Any, ci_provider, out change);
                         decimal.TryParse(tds[6].InnerText, NumberStyles.Any, ci_provider, out perchg);

                         list.Add(new FXStreetQuote(currency, last, open, high, low, change, perchg));
                     }
                     
                 }
             }
             return list;
         }

    }
}
 
   private void button1_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            list.Add("USDJPY");
            list.Add("GBPUSD");
            list.Add("USDCHF");
            list.Add("AUDUSD");
            list.Add("USDCAD");
            list.Add("NZDUSD");
            list.Add("USDCNY");
            list.Add("USDCNH");
            list.Add("USDHKD");
            list.Add("USDSGD");
            list.Add("USDNOK");
            list.Add("USDDKK");
            list.Add("USDSEK");
            list.Add("EURNOK");
            list.Add("EURDKK");
            list.Add("EURSEK");
            list.Add("EURCHF");
            list.Add("EURJPY");
            list.Add("EURGBP");
            list.Add("EURAUD");
            list.Add("GBPCHF");
            list.Add("GBPJPY");
            list.Add("GBPCAD");
            list.Add("GBPAUD");
            list.Add("AUDJPY");
            list.Add("CADJPY");
            list.Add("CHFJPY");
            list.Add("NZDJPY");
            list.Add("AUDCAD");
            list.Add("AUDCHF");
            list.Add("AUDSGD");

            FXStreet fx = new FXStreet(list);
            List<FXStreetQuote> result = new List<FXStreetQuote>();

            result = fx.Download();
           
        }

0

Użyłem powyższego kodu i dodałem mu funkcjonalność zapisuje do pliku tekstowego , jednakże wyniki które pobiera są jednakowe. Tzn. użyłem pętli i wywołuję listę result a potem realizuję zapis za pomocą streamu. Gdzie może tkwić problem?

1 użytkowników online, w tym zalogowanych: 0, gości: 1