none
HTML Inhalt auslesen RRS feed

  • Frage

  • Hallo Forum,

    ich lade eine HTML Seite und möchte nun bestimmte Informationen in Felder einlesen. Habe auch schon einiges versucht,

    aber so richtig bekomme ich das nicht hin.

    Wie kann man zB. aus folgendem HTML Code die Daten für "Hersteller" TeileNr, Mat Nr, EAN Code, ... auslesen ?

    Kann mir da jemand helfen ?

    Auszug HTML

    <div class="headinfo_left">
    	<table>
    
    		<tr class="odd">
    			<td>Hersteller</td>
    			<td>ASUS</td>
    		</tr>
    
    		<tr class="even">
    			<td>Hersteller Teile-Nr.</td>
    			<td>90NGKA318N37227T151Y</td>
    		</tr>
    
    		
    
    		<tr class="odd">
    			<td>Art.Nr.</td>
    			<td class="materialNo">34223764</td>
    		</tr>
    
    		<tr class="even">
    			<td>EAN-Code</td>
    			<td>4716659437840</td>
    		</tr>
    
    
    		
    
    
    		
    		
    		<tr class="odd">
    			<td>EVP (netto)</td>
    			<td>
    				<div class="priceXCV " id="listpric09534erewrds">
    					568,34 &euro;
    				</div>
    			</td>
    		</tr>


    Gruß Roland

    Montag, 2. Dezember 2013 09:03

Antworten

  • Hallo Roland,

    um HTML-Inhalte zu parsen, empfehle ich dir das Html Agility Pack.
    Es gibt dazu auch ein NuGet-Paket, das Du in Visual Studio bequem von der PM-Konsole installieren kannst. Nachfolgend ein Beispiel:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Collections;
    
    using HtmlAgilityPack;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string htmlToParse = "<divclass=\"headinfo_left\"><table><trclass=\"odd\"><td>Hersteller</td><td>ASUS</td></tr><trclass=\"even\"><td>HerstellerTeile-Nr.</td><td>90NGKA318N37227T151Y</td></tr><trclass=\"odd\"><td>Art.Nr.</td><td class=\"materialNo\">34223764</td></tr><trclass=\"even\"><td>EAN-Code</td><td>4716659437840</td></tr><trclass=\"odd\"><td>EVP(netto)</td><td><divclass=\"priceXCV\" id=\"listpric09534erewrds\">568,34&euro;</div></td></tr>";
    
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(htmlToParse);
    
                Hashtable table = new Hashtable();
    
                var tdValues = doc.DocumentNode.SelectNodes("//td").Select(n => n.InnerText).ToArray();
    
                for(int i = 0; i < tdValues.Length - 1; i+=2)
                    table.Add(tdValues[i], tdValues[i+1]);
    
                foreach(var key in table.Keys)
                    Console.WriteLine("{0}={1}", key, table[key]);
                
                Console.ReadKey(true);
            }
        }
    }
    

    Gruß
    Marcel

    Montag, 2. Dezember 2013 10:04
    Moderator
  • Hi Roland,ein geeignets mittel ist http://htmlagilitypack.codeplex.com/ damit solltest du das Problem lösen können.

    Montag, 2. Dezember 2013 10:08

Alle Antworten

  • Hallo Roland,

    um HTML-Inhalte zu parsen, empfehle ich dir das Html Agility Pack.
    Es gibt dazu auch ein NuGet-Paket, das Du in Visual Studio bequem von der PM-Konsole installieren kannst. Nachfolgend ein Beispiel:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Collections;
    
    using HtmlAgilityPack;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string htmlToParse = "<divclass=\"headinfo_left\"><table><trclass=\"odd\"><td>Hersteller</td><td>ASUS</td></tr><trclass=\"even\"><td>HerstellerTeile-Nr.</td><td>90NGKA318N37227T151Y</td></tr><trclass=\"odd\"><td>Art.Nr.</td><td class=\"materialNo\">34223764</td></tr><trclass=\"even\"><td>EAN-Code</td><td>4716659437840</td></tr><trclass=\"odd\"><td>EVP(netto)</td><td><divclass=\"priceXCV\" id=\"listpric09534erewrds\">568,34&euro;</div></td></tr>";
    
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(htmlToParse);
    
                Hashtable table = new Hashtable();
    
                var tdValues = doc.DocumentNode.SelectNodes("//td").Select(n => n.InnerText).ToArray();
    
                for(int i = 0; i < tdValues.Length - 1; i+=2)
                    table.Add(tdValues[i], tdValues[i+1]);
    
                foreach(var key in table.Keys)
                    Console.WriteLine("{0}={1}", key, table[key]);
                
                Console.ReadKey(true);
            }
        }
    }
    

    Gruß
    Marcel

    Montag, 2. Dezember 2013 10:04
    Moderator
  • Hi Roland,ein geeignets mittel ist http://htmlagilitypack.codeplex.com/ damit solltest du das Problem lösen können.

    Montag, 2. Dezember 2013 10:08