Autor Beitrag
g!ml!
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16


[C#] [VB.NET]
BeitragVerfasst: Di 23.09.08 09:08 
Hallo zusammen,
ich soll aus bestimmten Internetseiten den Endpreis des Artikels rausfiltern mit einer im Programm eingegebenen OE-Nummer. Ich hab es so weit, dass ich den HTML-Code in die ListView bekomme...

so sieht das ganze aus:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.IO;

namespace OENR__Windows_Applikation_
{
    public partial class OENRForm : Form
    {
        public OENRForm()
        {
            InitializeComponent();
        }

        public void OENR_Anzeigen_b_Click(object sender, EventArgs e)
        {
            string sOENR = OENR_txtbx.Text;
            WebClient Client = new WebClient();

            string sHTML_Code_Alanko = Client.DownloadString("http://www.alanko.de/do_search_oenr.php?action=search&oenr=" + sOENR + "");;
            Preise_lstvw.Items.Add(sHTML_Code_Alanko);

            string sHTML_Code_Yabazzo = Client.DownloadString("http://yabazzo.de/php/trippshop.php?mode=search&search[string]=" + sOENR + "&submit2=jetzt+suchen");
            Preise_lstvw.Items.Add(sHTML_Code_Yabazzo);

            string sHTML_Code_Preiswerte_Anlasser = Client.DownloadString("http://preiswerte-anlasser.de/index.php?productid=" + sOENR);
            Preise_lstvw.Items.Add(sHTML_Code_Preiswerte_Anlasser);
        }
    }
}



jetzt muss ich nurnoch eine bestimmten Teilstring rausziehen, und zwar den Endpreis...
Ich muss nun also den HTML-Code der Seite des Artikels nach dem Endpreis (der mit anderer Artikelnummer natürlich immer abweicht) durchsuchen und dann in die ListView eintragen. Ich komme momentan nicht weiter. Ich wäre dankbar für eure Hilfe.

mfg

g!ml!

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Di 23.09.08 09:48 
Hallo,

die Seiten sind dann HTML-Code. Also stehen in dem String sHTML_Code_Alanko irgendwo und irgendwie Elemente der folgenden Art:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
<span class="strong">04.15</span>
//  oder
<td nowrap><span class="strong">HARMONIA MUNDI FRANCE</span>
LC 07045Best.Nr HMC 901767</td>
//  oder
<div>04.15</div>

Auf jeden Fall musst Du die HTML-Seite manuell durchsuchen nach dem Schnipsel, der den Preis enthält. Ob das besser über Methoden der String-Klasse (IndexOf, Substring u.ä.) oder über den XmlReader-Klasse geht, hängt von der Struktur der Seite ab sowie von der Art der Daten, die Du herausholen willst.

Jürgen
g!ml! Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16


[C#] [VB.NET]
BeitragVerfasst: Di 23.09.08 13:13 
Ich habe das Problem gelöst!

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.IO;

namespace OENR__Windows_Applikation_
{
    public partial class OENRForm : Form
    {
        public OENRForm()
        {
            InitializeComponent();
        }

        public void OENR_Anzeigen_b_Click(object sender, EventArgs e)
        {
            string sOENR = OENR_txtbx.Text;
            WebClient Client = new WebClient();
            string sAlanko = "";
            string sYabazzo = "";
            string Path = "C:\\Dokumente und Einstellungen\\Funke\\Eigene Dateien\\Eurotec.txt";

            ListViewItem LVI = new ListViewItem();

            /*if (File.Exists("@" + Path))
            {
                MessageBox.Show("Die Datei existierte bereits, und wurde nicht erstellt!");
            }
            else
            {
                File.Create("@" + Path);
            }*/


            StreamReader StrRea = new StreamReader(Path);
            string EurotecText = StrRea.ReadLine();

            using (StrRea)
            {
                Preise_lstvw.Items.Add(EurotecText);
            }

            string sHTML_Code_Alanko = Client.DownloadString("http://www.alanko.de/do_search_oenr.php?action=search&oenr=" + sOENR + "");
            
            for (int iPosition_Alanko = 10; iPosition_Alanko <= sHTML_Code_Alanko.Length; iPosition_Alanko++)
            {
                if(sHTML_Code_Alanko.Substring(iPosition_Alanko, 3).Contains("EUR"))
                {
                    sAlanko = sHTML_Code_Alanko.Substring(iPosition_Alanko - 76);
                    break;
                }
            }

            LVI.SubItems.Add(sAlanko);
            Preise_lstvw.Items.Add(LVI);

            string sHTML_Code_Yabazzo = Client.DownloadString("http://yabazzo.de/php/trippshop.php?mode=search&search[string]=" + sOENR + "&submit2=jetzt+suchen");

            for (int iPosition_Yabazzo = 0; iPosition_Yabazzo <= sHTML_Code_Yabazzo.Length; iPosition_Yabazzo++)
            {
                if (sHTML_Code_Yabazzo.Substring(iPosition_Yabazzo, 3).Contains("€"))
                {
                    sYabazzo = sHTML_Code_Yabazzo.Substring(iPosition_Yabazzo - 46);
                    break;
                }
            }

            LVI.SubItems.Add(sYabazzo);
        }
    }
}


Moderiert von user profile iconKha: C#-Tags hinzugefügt
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Di 23.09.08 14:06 
Hallo,

so mag es gehen, aber ich möchte Dich noch etwas bremsen.

1. Nach "EUR" zu suchen ist riskant, weil diese Buchstaben auch noch in anderem Zusammenhang auftauchen könnten. Da in HTML überwiegend mit Kleinbuchstaben gearbeitet wird, sind Verwechslungen zwar nicht sehr wahrscheinlich, aber dennoch möglich.

2. Deine Suche mit Contains() und einer for-Schleife dauert sehr lange, weil Du von jeder Position aus Substring und Contains benutzt. Besser ist es, mit IndexOf direkt an eine bestimmte Stelle zu springen und dann die Textteile davor herauszuholen; z.B. die 10 letzten Zeichen davor über (!Char.IsLetter) prüfen.

Außerdem sieht der using-Block etwas seltsam aus. Ich kenne es so:
ausblenden C#-Quelltext
1:
2:
3:
4:
using(StreamReader StrRea = new StreamReader(Path)) {
    string EurotecText = StrRea.ReadLine(); 
    Preise_lstvw.Items.Add(EurotecText); 
}

Bist Du sicher, dass Du nur die erste Zeile lesen willst? Bei mehreren Zeilen geht es auch so:
ausblenden C#-Quelltext
1:
Preise_lstvw.Items.AddRange( File.ReadAllLines(Path) );					

Die File.Read-Methoden sind meistens praktischer.

Außerdem möchte ich davor warnen, einen Bezeichner für Klassen und Eigenschaften (hier: Path) als Variablennamen zu verwenden; das führt leicht zu Missverständnissen.

Gruß Jürgen
g!ml! Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16


[C#] [VB.NET]
BeitragVerfasst: Mi 24.09.08 10:53 
Danke an Jürgen, so hat es hingehauen!

Nunja, jetzt habe ich ein anderes Problem, ich lese die OE-Nummern aus einem Textfile aus, und dass soll in einer Schleife Reihe für Reihe passieren, aber anscheinend schließt sich der StreamReader nach dem ersten Durchgang der while-Schleife wieder...

so sieht das ganze aus:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.IO;

namespace OENR_mit_Schleife__Win_App_
{
    public partial class OENRmitSchleife : Form
    {
        public OENRmitSchleife()
        {
            InitializeComponent();
        }

        private void Anzeigen_b_Click(object sender, EventArgs e)
        {
            string sGesamteZeile = "";
            WebClient Client = new WebClient();
            string sEurotec = "";
            string sAlanko = "";
            string sYabazzo = "";
            string EurotecPath = "C:\\Dokumente und Einstellungen\\Funke\\Eigene Dateien\\Eurotec.txt";
            string OENRPath = "C:\\Dokumente und Einstellungen\\Funke\\Eigene Dateien\\OE-Nummern.txt";

            ListViewItem LVI = new ListViewItem();

            StreamReader StrReaOENummern = new StreamReader(OENRPath);

            sGesamteZeile = StrReaOENummern.ReadLine();
            string[] sOENR = sGesamteZeile.Split(new Char[] { ';' });
            
            while (!StrReaOENummern.EndOfStream == true)
            {
                StreamReader StrReaEurotec = new StreamReader(EurotecPath);

                sEurotec = StrReaEurotec.ReadLine();
                
                using (StrReaOENummern)
                {
                    LVI.Text = sEurotec;
                }

                StrReaEurotec.Close();
                
                string sHTML_Code_Alanko = Client.DownloadString("http://www.alanko.de/do_search_oenr.php?action=search&oenr=" + sOENR[0] + "");
                int Index_Alanko = sHTML_Code_Alanko.IndexOf("EUR");
                sAlanko = sHTML_Code_Alanko.Substring(Index_Alanko - 76);

                string sHTML_Code_Yabazzo = Client.DownloadString("http://yabazzo.de/php/trippshop.php?mode=search&search[string]=" + sOENR[0] + "&submit2=jetzt+suchen");
                int Index_Yabazzo = sHTML_Code_Yabazzo.IndexOf("€</h1>");
                sYabazzo = sHTML_Code_Yabazzo.Substring(Index_Yabazzo - 66);

                LVI.SubItems.Add(sAlanko);
                LVI.SubItems.Add(sYabazzo);
                Preise_lstvw.Items.Add(LVI);
            }
        }

wäre für jede Hilfe dankbar!
mfg
g!ml!


Zuletzt bearbeitet von g!ml! am Mi 24.09.08 11:06, insgesamt 1-mal bearbeitet
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mi 24.09.08 10:55 
Hallo,

so ist der Code doch viel besser lesbar, nicht wahr?

Mir fallen drei fragwürdige Punkte auf:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
           StreamReader StrReaOENummern = new StreamReader(OENRPath);
           sGesamteZeile = StrReaOENummern.ReadLine();
            
            while (!StrReaOENummern.EndOfStream == true)
            {
                using (StrReaOENummern)
                {
                    LVI.Text = sEurotec;
                }

Was soll dieser using-Block bedeuten? Siehe meinen obigen Hinweis auf die "richtige" Konstruktion. Ich halte es für möglich, dass StrReaOENummern am Ende des using-Blocks geschlossen wird; damit würde auch die while-Schleife nur einmal durchlaufen.

ausblenden C#-Quelltext
1:
StrReaOENummern.ReadLine();					

Dieser Befehl taucht nur einmal auf, aber er gehört auch zur while-Schleife. Beides wird üblicherweise so verknüpft (siehe das Beispiel aus der SDK-Doku/MSDN:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
            using (StreamReader sr = new StreamReader(path)) 
            {
                while (sr.Peek() >= 0
                {
                    string line = sr.ReadLine();
                    //  und mach was mit diesem string
                }
            }


ausblenden C#-Quelltext
1:
LVI = new ListViewItem();					

Auch das scheint zur while-Schleife zu gehören: Für jeden Eintrag in der OENummern-Datei ist ein neuer Eintrag (= new) zu erstellen.

Mir ist außerdem unklar, warum Du die beiden StreamReader unterschiedlich benutzt, statt sie einheitlich mit using zu kapseln. Abgesehen davon ist (wie schon vorgeschlagen) File.Read praktischer.

Jürgen
g!ml! Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16


[C#] [VB.NET]
BeitragVerfasst: Do 25.09.08 08:26 
So ist der momentane Stand der Dinge:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.IO;

namespace OENR_mit_Schleife__Win_App_
{
    public partial class OENRmitSchleife : Form
    {
        string sAlanko = "";
        string sYabazzo = "";
        string[,] sDatenString = new string[10050];
        string[] sOENR = new string[100];

        public OENRmitSchleife()
        {
            InitializeComponent();
            
            Preise_lstvw.Items.Clear();
            sAlanko = "";
            sYabazzo = "";

            for (int y = 0; y < sDatenString.GetLength(0); y++)
            {
                for (int x = 0; x < sDatenString.GetLength(1); x++)
                {
                    sDatenString[y, x] = "";
                }
            }
        }

        public void Anzeigen_b_Click(object sender, EventArgs e)
        {
            Preise_lstvw.Items.Clear();
            
            if (OpFiDi.ShowDialog() == DialogResult.OK)
            {
                string OENRPath = OpFiDi.FileName;
                string sGesamteZeile = "";

                WebClient Client = new WebClient();

                StreamReader StrReaOENummern = new StreamReader(OENRPath);

                sGesamteZeile = StrReaOENummern.ReadToEnd();
                sOENR = sGesamteZeile.Split(new Char[] {'\r'});

                for (int i = 0; i < sOENR.Length; i++)
                {
                    ListViewItem LVI = new ListViewItem();

                    string s = sOENR[i];
                    string[] sort = s.Split(new Char[] {';'});

                    sort[0] = sort[0].Replace("\n""");

                    using (StrReaOENummern)
                    {
                        LVI.Text = sOENR[i];
                    }

                    string sHTML_Code_Alanko = Client.DownloadString("http://www.alanko.de/do_search_oenr.php?action=search&oenr=" + sort[0] + "");
                    int Index_Alanko = sHTML_Code_Alanko.IndexOf("EUR");

                    if (Index_Alanko < 0)
                    {
                        sAlanko = "N/A";
                    }

                    else
                    {
                        sAlanko = sHTML_Code_Alanko.Substring(Index_Alanko - 76);
                    }

                    string sHTML_Code_Yabazzo = Client.DownloadString("http://yabazzo.de/php/trippshop.php?mode=search&search[string]=" + sort[0] + "&submit2=jetzt+suchen");
                    int Index_Yabazzo = sHTML_Code_Yabazzo.IndexOf("€</h1>");

                    if (Index_Yabazzo < 0)
                    {
                        sYabazzo = "N/A";
                    }

                    else
                    {
                        sYabazzo = sHTML_Code_Yabazzo.Substring(Index_Yabazzo - 66);
                    }

                    sOENR[i] = sOENR[i].Replace("\n""");

                    LVI.Text = sOENR[i];

                    LVI.SubItems.Add(sAlanko);
                    LVI.SubItems.Add(sYabazzo);
                    Preise_lstvw.Items.Add(LVI);

                    sDatenString[i, 0] = sOENR[i];
                    sDatenString[i, 1] = sAlanko;
                    sDatenString[i, 2] = sYabazzo;
                }

            }
        }

        public void InTXTextrahieren_b_Click(object sender, EventArgs e)
        {
            if (SaFiDi.ShowDialog() == DialogResult.OK)
            {
                string DatenPath = SaFiDi.FileName;

                if (File.Exists(DatenPath))
                {

                }
                
                else
                {
                    File.Create(DatenPath);
                }

                StreamWriter StrWriDaten = new StreamWriter(DatenPath);

                using (StrWriDaten)
                {
                    StrWriDaten.Write(sDatenString[00] = "OE-Nummer;Eurotec;Alanko;Yabazzo\r\n");

                    for (int y = 1; y < sOENR.Length; y++)
                    {
                        StrWriDaten.Write(sDatenString[y, 0] + ";" + sDatenString[y, 1] + ";" + sDatenString[y, 2] + "\r\n");
                    }
                }

                Preise_lstvw.Items.Clear();

                sAlanko = "";
                sYabazzo = "";

                for (int y = 0; y < sDatenString.GetLength(0); y++)
                {
                    for (int x = 0; x < sDatenString.GetLength(1); x++)
                    {
                        sDatenString[y, x] = "";
                    }
                }
            }
        }
    }
}
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Do 25.09.08 09:55 
Und was willst Du uns mit Deinem letzten Beitrag sagen?

Was den StreamReader und "seinen" using-Block betrifft, willst Du Dich wohl als beratungsresistent zeigen. Nun ja :roll:

Jürgen
g!ml! Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16


[C#] [VB.NET]
BeitragVerfasst: Do 25.09.08 10:55 
hmmm, es funktioniert so ^^ wieso sollte ich das dann ändern?
mfg
g!ml!
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Do 25.09.08 12:17 
user profile icong!ml! hat folgendes geschrieben:
hmmm, es funktioniert so ^^ wieso sollte ich das dann ändern?
Du könntest auch ein if (true) ; in deinen Code einfügen und er würde wohl immer noch funktionieren. Macht aber genauso wenig Sinn wie dein using-Block :nixweiss: . Und nebenbei stellt es natürlich noch in Frage, ob du eigentlich verstanden hast, was ein using-Block tut ;) .

_________________
>λ=
g!ml! Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16


[C#] [VB.NET]
BeitragVerfasst: Mi 08.10.08 11:46 
Vielen Dank für eure Hilfe, ich habe es nun am Laufen!

mfg
g!ml!