Webseiteninhalt auslesen und in csv Datei schreiben

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Antarez
User
Beiträge: 2
Registriert: Samstag 19. September 2015, 17:39

Hallo Phytongemeinde

Ich bin Python Neuling und tue mich etwas schwer mit dem Auslesen eines bestimmten Webseiteninhalts.

Worum geht es genau:

Ich habe einen Drucker auf dessen WebGUI sich auf mehrere Seiten verteilt die Seitenzahlen von Farb- und Schwarzweissdruck, Kopie und Scan auslesen lassen und zwar
für verschiedene ID Codes wie z.B. 101, 102 und 999 wie im HTML Auszug unten ersichtlich.

Wie ich den HTML Code mit Python auslesen kann weiss ich (habe mir dazu das Python Programming Tutorial 25-27 How to build a webcrawler von Thenewboston auf youtube durchgearbeitet).
Da die Struktur der Auszulesenden Webseite doch etwas komplizierter ist als diejenige die im Tutorial beschrieben wurde, stehe ich etwas am Berg.
Ich habe es bereits mit VBA in Excel und Regular expressions versucht, bin da aber nicht auf eine geeignete Lösung gestossen und frage mich nun, ob es eventuell in Python gar einfacher möglich wäre.

Wie ich die gewünschten Inhalte dann in die CSV Datei speichern kann, weiss ich im Moment nur bedingt, wenn Ihr mir aber schon mal beim Durchscannen der Seite und dem Finden und Zuweisen der Inhalte (Drucksummen, Kopiensummen, Schwarzkopien, Farbkopien zur entsprechenden ID) behilflich sein könntet wäre mir schon echt geholfen.

Die fixen URLs der verschiedenen Seiten von 0 bis 9 sind bekannt, ich kann mir diese denke ich in ein Listenelement einlesen und abarbeiten lassen.

{
document.writeln('<colgroup>');
( "true" != NewCardStat ) ? document.writeln('<col class="SelectColumn" \/>') : document.writeln('<col class="BumonIDColumn" \/>');
( "true" != NewCardStat ) ? document.writeln('<col class="BumonIDColumn" \/>') : document.writeln('<col class="CardIDColumn" \/>');
document.writeln('<col class="TotalPrintColumn" \/>');
document.writeln('<col class="TotalColorPrintColumn" \/>');
document.writeln('<col class="TotalMonocloPrintColumn" \/>');
document.writeln('<col class="ColorCopyColumn" \/>');
document.writeln('<col class="ColorScanColumn" \/>');
document.writeln('<col class="ColorPrintColumn" \/>');
document.writeln('<col class="MonocloCopyColumn" \/>');
document.writeln('<col class="MonocloScanColumn" \/>');
document.writeln('<col class="MonocloPrintColumn" \/>');
document.writeln('<\/colgroup>');
document.writeln('<thead>');
document.writeln('<tr>');
document.writeln('<th rowspan="2" colspan="2"><\/th>');
document.writeln('<th colspan="9">Gesamtseiten/Seitenbeschränkungen <\/th>');
document.writeln('<\/tr>');
document.writeln('<tr>');
document.writeln('<th colspan="3">Total <\/th>');
document.writeln('<th colspan="3">Farbe <\/th>');
document.writeln('<th colspan="3">Schwarz <\/th>');
document.writeln('<\/tr>');
document.writeln('<tr>');
document.writeln('<th>');
( "true" != NewCardStat ) ? document.writeln('Auswählen ') : document.writeln('Abt. ID ');
document.writeln('<\/th>');
document.writeln('<th>');
( "true" != NewCardStat ) ? document.writeln('Abt. ID ') : document.writeln('Karten ID ');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Drucksummen');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Total (Farbe)');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Total (Schwarz)');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Farbkopie');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Farbscan');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Farbdruck');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Schwarzkopie');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Schwarzscan');
document.writeln('<\/th>');
document.writeln('<th>');
document.writeln('Schwarzdruck');
document.writeln('<\/th>');
document.writeln('<\/tr>');
document.writeln('<\/thead>');
}
//]]>
</script>


</script>
<fieldset class="FilterOperation">
<script language="JavaScript" type="text/javascript">
//<![CDATA[
document.writeln('<select name="Page" id="Page" size="1">');
document.writeln('<option value="0">1 bis 100 <\/option>');
document.writeln('<option value="1">101 bis 200 <\/option>');
document.writeln('<option value="2">201 bis 300 <\/option>');
document.writeln('<option value="3">301 bis 400 <\/option>');
document.writeln('<option value="4">401 bis 500 <\/option>');
document.writeln('<option value="5">501 bis 600 <\/option>');
document.writeln('<option value="6">601 bis 700 <\/option>');
document.writeln('<option value="7">701 bis 800 <\/option>');
document.writeln('<option value="8">801 bis 900 <\/option>');
document.writeln('<option value="9">901 bis 1002 <\/option>');
document.writeln('<\/select>');
document.writeln('<input type="button" class="ButtonEnable" value="Anzeige " onclick="javascript:reload_click();" \/>');
DropDownListSelect( document.PROP_FORM.Page, "0" );
//]]>
</script>
</fieldset>
</div>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
document.writeln('<table summary="">');
( "true" == has_clrprt ) ? ListMenuDraw_Cl() : ListMenuDraw_Bw() ;
document.writeln('<tbody>');
document.writeln('<tr>');
if( "true" != NewCardStat )
{
document.writeln('<td>');
document.writeln('<input type="radio" class="Radio" name="SecIdBtn" id="SecIdBtn" value="101" onclick="Callback_SecIdBtn( this.value )" \/>');
document.writeln('<\/td>');
}
document.writeln('<td>');
document.writeln('<a href="javascript:Go_DetailPg(\'101\');">');
document.writeln('101');
document.writeln('<\/a>');
document.writeln('<\/td>');
if( "true" == NewCardStat )
{
document.writeln('<td>');
document.writeln('');
document.writeln('<\/td>');
}
if( "true" == has_clrprt )
{
Draw_CounterLimit( "0", "0", "0", "false" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "false" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "false" );
}
else
{
Draw_CounterLimit( "0", "0", "0", "false" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "false" );
}
document.writeln('<\/tr>');
document.writeln('<tr>');
if( "true" != NewCardStat )
{
document.writeln('<td>');
document.writeln('<input type="radio" class="Radio" name="SecIdBtn" id="SecIdBtn" value="102" onclick="Callback_SecIdBtn( this.value )" \/>');
document.writeln('<\/td>');
}
document.writeln('<td>');
document.writeln('<a href="javascript:Go_DetailPg(\'102\');">');
document.writeln('102');
document.writeln('<\/a>');
document.writeln('<\/td>');
if( "true" == NewCardStat )
{
document.writeln('<td>');
document.writeln('');
document.writeln('<\/td>');
}
if( "true" == has_clrprt )
{
Draw_CounterLimit( "0", "0", "0", "false" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "false" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "false" );
}
else
{
Draw_CounterLimit( "0", "0", "0", "false" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "true" );
Draw_CounterLimit( "0", "0", "0", "false" );
}
document.writeln('<\/tr>');
document.writeln('<tr>');
if( "true" != NewCardStat )
{
document.writeln('<td>');
document.writeln('<input type="radio" class="Radio" name="SecIdBtn" id="SecIdBtn" value="999" onclick="Callback_SecIdBtn( this.value )" \/>');
document.writeln('<\/td>');
}
document.writeln('<td>');
document.writeln('<a href="javascript:Go_DetailPg(\'999\');">');
document.writeln('999');
document.writeln('<\/a>');
document.writeln('<\/td>');
if( "true" == NewCardStat )
{
document.writeln('<td>');
document.writeln('');
document.writeln('<\/td>');
}
if( "true" == has_clrprt )
{
Draw_CounterLimit( "2530", "0", "0", "false" );
Draw_CounterLimit( "1104", "0", "0", "true" );
Draw_CounterLimit( "1426", "0", "0", "true" );
Draw_CounterLimit( "29", "0", "0", "true" );
Draw_CounterLimit( "7", "0", "0", "true" );
Draw_CounterLimit( "1075", "0", "0", "false" );
Draw_CounterLimit( "105", "0", "0", "true" );
Draw_CounterLimit( "2", "0", "0", "true" );
Draw_CounterLimit( "1321", "0", "0", "false" );
}
else
{
Draw_CounterLimit( "2530", "0", "0", "false" );
Draw_CounterLimit( "7", "0", "0", "true" );
Draw_CounterLimit( "105", "0", "0", "true" );
Draw_CounterLimit( "2", "0", "0", "true" );
Draw_CounterLimit( "1321", "0", "0", "false" );
}

Für Eure Inputs schon mal rechten Dank.

Grüsse Antarez
Sirius3
User
Beiträge: 17754
Registriert: Sonntag 21. Oktober 2012, 17:20

@Antarez: Du mußt Dir halt die Zeilen mit Draw_CounterLimit herausfiltern und den Spalten zuordnen.
Antarez
User
Beiträge: 2
Registriert: Samstag 19. September 2015, 17:39

Sirius3 hat geschrieben:@Antarez: Du mußt Dir halt die Zeilen mit Draw_CounterLimit herausfiltern und den Spalten zuordnen.
Hallo Sirius3

Danke für Deinen Hinweis. Das möchte ich gerne tun, nur weiss ich nicht genau wie ich diese dann dem ID Code zuordnen kann.
Der ID Code befindet sich ja z.B. in diesem Abschnitt

document.writeln('101');

Danach kommen die Draw_CounterLimit Einträge (9 an der Zahl) nach der else kommen nochmal 5 Einträge, die kann ich aber vernachlässigen, da dies nur nochmals eine andere Zusammenfassung der Zahlen ist. Wie würdest Du eine solche Schleife in Python programmieren? Wie gesagt ich bin noch ein Newbie und erst dabei die Sprache zu lernen.

Danke Dir vielmals für Deinen Hinweis.

Grüsse Antarez
Benutzeravatar
pixewakb
User
Beiträge: 1412
Registriert: Sonntag 24. April 2011, 19:43

Das ist JavaScript, das eine Tabelle erzeugt (ohne es zu testen). Warum schaust du dir das JavaScript an und nicht die fertigen, statischen HTML-Seite? Bitte zusätzlich beim ersten Post den JS-Code in Code-Tags setzen.
BlackJack

@pixewakb: Ähm, das JavaScript kommt vom Webserver und eben nicht die fertige Seite. Wenn man da also Daten scrapen will, dann muss man das schon aus dem JavaScript holen, denn HTML gibt's ja nicht. Oder man muss das JavaScript ausführen und dann das HTML-Dokument scrapen. Keine Ahnung ob's eine gute Anbindung an so etwas wie PhantomJS gibt.
Antworten