<div></div>Salve a tutti sono un nuovo iscritto alla mailing lista.<div>Ho bisogno di un aiuto per capire come andare avanti in un esercizio assegnatoci all'universita'.</div><div>In pratica devo creare una classe che preso in input un file HTML mi genera l'albero Html della stessa e poi farci diverse operazioni sopra come contare nodi e cose simili.</div><div>Ho gia' fatto un esercizio simile ma senza usare due classi ma usando una classe sola e una funzione esterna che gli passa il file HTML e va bene.</div><div>Purtroppo quando sostituisco alla funzione esterna la classe che mi chiede l'esercizio iniziano i problemi.</div><div>Metto l'esempio funzionante</div><div><pre style="font-family: 'Bitstream Vera Sans Mono', 'Courier New', Monaco, monospace; font-size: 14px; color: rgb(0, 0, 0);"><span class="kn" style="color: rgb(0, 128, 0); font-weight: bold;">import</span> <span class="nn" style="color: rgb(0, 0, 255); font-weight: bold;">html</span>
<a name="line-2" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a>
<a name="line-3" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a><span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">class</span> <span class="nc" style="color: rgb(0, 0, 255); font-weight: bold;">HTMLNode</span><span class="p">(</span><span class="nb" style="color: rgb(0, 128, 0);">object</span><span class="p">):</span>
<a name="line-4" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">def</span> <span class="nf" style="color: rgb(0, 0, 255);">__init__</span><span class="p">(</span><span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">attr</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">closed</span><span class="o" style="color: rgb(102, 102, 102);">=</span><span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">True</span><span class="p">):</span>
<a name="line-5" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">tag</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="n">tag</span>
<a name="line-6" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">attr</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="n">attr</span> <span class="c" style="color: rgb(64, 128, 128); font-style: italic;"># dizionario degli attributi, se non ci</span>
<a name="line-7" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="c" style="color: rgb(64, 128, 128); font-style: italic;"># sono, e' un dizionario vuoto</span>
<a name="line-8" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">content</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="n">content</span> <span class="c" style="color: rgb(64, 128, 128); font-style: italic;"># se tag = '_text_' contiene il testo,</span>
<a name="line-9" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="c" style="color: rgb(64, 128, 128); font-style: italic;"># altrimenti una lista dei nodi figli</span>
<a name="line-10" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">closed</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="n">closed</span> <span class="c" style="color: rgb(64, 128, 128); font-style: italic;"># True se il nodo ha la chiusura</span>
<a name="line-11" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a>
<a name="line-12" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">def</span> <span class="nf" style="color: rgb(0, 0, 255);">istext</span><span class="p">(</span><span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="p">):</span> <span class="c" style="color: rgb(64, 128, 128); font-style: italic;"># per distinguere i nodi testo</span>
<a name="line-13" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">return</span> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">tag</span> <span class="o" style="color: rgb(102, 102, 102);">==</span> <span class="s" style="color: rgb(186, 33, 33);">'_text_'</span>
<a name="line-14" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a>
<a name="line-15" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">def</span> <span class="nf" style="color: rgb(0, 0, 255);">print_tree</span><span class="p">(</span><span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="p">,</span> <span class="n">level</span><span class="o" style="color: rgb(102, 102, 102);">=</span><span class="mi" style="color: rgb(102, 102, 102);">0</span><span class="p">):</span>
<a name="line-16" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="sd" style="color: rgb(186, 33, 33); font-style: italic;">'''Stampa l'albero mostrando la struttura tramite indentazione'''</span>
<a name="line-17" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="nb" style="color: rgb(0, 128, 0);">print</span> <span class="s" style="color: rgb(186, 33, 33);">' '</span><span class="o" style="color: rgb(102, 102, 102);">*</span><span class="n">level</span><span class="o" style="color: rgb(102, 102, 102);">+</span><span class="nb" style="color: rgb(0, 128, 0);">str</span><span class="p">(</span><span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="p">),</span>
<a name="line-18" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">if</span> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">istext</span><span class="p">():</span>
<a name="line-19" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="nb" style="color: rgb(0, 128, 0);">print</span> <span class="nb" style="color: rgb(0, 128, 0);">repr</span><span class="p">(</span><span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">content</span><span class="p">)</span>
<a name="line-20" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">else</span><span class="p">:</span>
<a name="line-21" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="nb" style="color: rgb(0, 128, 0);">print</span>
<a name="line-22" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">for</span> <span class="n">node</span> <span class="ow" style="color: rgb(170, 34, 255); font-weight: bold;">in</span> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">content</span><span class="p">:</span>
<a name="line-23" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="n">node</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">print_tree</span><span class="p">(</span><span class="n">level</span><span class="o" style="color: rgb(102, 102, 102);">+</span><span class="mi" style="color: rgb(102, 102, 102);">1</span><span class="p">)</span>
<a name="line-24" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a>
<a name="line-25" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">def</span> <span class="nf" style="color: rgb(0, 0, 255);">__str__</span><span class="p">(</span><span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="p">):</span>
<a name="line-26" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="sd" style="color: rgb(186, 33, 33); font-style: italic;">'''Ritorna una rapprentazione testuale del nodo'''</span>
<a name="line-27" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="n">left</span><span class="p">,</span> <span class="n">right</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="p">(</span><span class="s" style="color: rgb(186, 33, 33);">''</span><span class="p">,</span> <span class="s" style="color: rgb(186, 33, 33);">''</span><span class="p">)</span> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">if</span> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">tag</span> <span class="o" style="color: rgb(102, 102, 102);">==</span> <span class="s" style="color: rgb(186, 33, 33);">'_text_'</span> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">else</span> <span class="p">(</span><span class="s" style="color: rgb(186, 33, 33);">'<'</span><span class="p">,</span> <span class="s" style="color: rgb(186, 33, 33);">'>'</span><span class="p">)</span>
<a name="line-28" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="n">s</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="n">left</span><span class="o" style="color: rgb(102, 102, 102);">+</span><span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">tag</span>
<a name="line-29" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow" style="color: rgb(170, 34, 255); font-weight: bold;">in</span> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">attr</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">items</span><span class="p">():</span>
<a name="line-30" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="n">s</span> <span class="o" style="color: rgb(102, 102, 102);">+=</span> <span class="s" style="color: rgb(186, 33, 33);">' '</span><span class="o" style="color: rgb(102, 102, 102);">+</span><span class="n">k</span><span class="o" style="color: rgb(102, 102, 102);">+</span><span class="s" style="color: rgb(186, 33, 33);">'="'</span><span class="o" style="color: rgb(102, 102, 102);">+</span><span class="n">v</span><span class="o" style="color: rgb(102, 102, 102);">+</span><span class="s" style="color: rgb(186, 33, 33);">'"'</span>
<a name="line-31" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">return</span> <span class="n">s</span> <span class="o" style="color: rgb(102, 102, 102);">+</span> <span class="n">right</span>
<a name="line-32" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a>
<a name="line-33" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">def</span> <span class="nf" style="color: rgb(0, 0, 255);">count</span><span class="p">(</span><span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="p">):</span>
<a name="line-34" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="sd" style="color: rgb(186, 33, 33); font-style: italic;">'''Ritorna il numero di nodi dell'albero'''</span>
<a name="line-35" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="n">cnt</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="mi" style="color: rgb(102, 102, 102);">1</span>
<a name="line-36" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">if</span> <span class="ow" style="color: rgb(170, 34, 255); font-weight: bold;">not</span> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">istext</span><span class="p">():</span>
<a name="line-37" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">for</span> <span class="n">node</span> <span class="ow" style="color: rgb(170, 34, 255); font-weight: bold;">in</span> <span class="bp" style="color: rgb(0, 128, 0);">self</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">content</span><span class="p">:</span>
<a name="line-38" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="n">cnt</span> <span class="o" style="color: rgb(102, 102, 102);">+=</span> <span class="n">node</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">count</span><span class="p">()</span>
<a name="line-39" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">return</span> <span class="n">cnt</span>
<a name="line-40" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a>
<a name="line-41" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a><span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">def</span> <span class="nf" style="color: rgb(0, 0, 255);">ParsedHTML</span><span class="p">(</span><span class="n">htmlfile</span><span class="p">):</span>
<a name="line-42" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a><span class="sd" style="color: rgb(186, 33, 33); font-style: italic;">'''Esegue il parsing HTML del file fhtml e ritorna la radice</span>
<a name="line-43" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a><span class="sd" style="color: rgb(186, 33, 33); font-style: italic; background: rgb(255, 255, 153);">dell'albero di parsing'''</span>
<a name="line-44" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">with</span> <span class="nb" style="color: rgb(0, 128, 0);">open</span><span class="p">(</span><span class="n">htmlfile</span><span class="p">,</span> <span class="s" style="color: rgb(186, 33, 33);">'U'</span><span class="p">)</span> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">as</span> <span class="n">f</span><span class="p">:</span>
<a name="line-45" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="n">root</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="n">html</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">parse</span><span class="p">(</span><span class="n">f</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">read</span><span class="p">(),</span> <span class="n">HTMLNode</span><span class="p">)</span>
<a name="line-46" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a> <span class="k" style="color: rgb(0, 128, 0); font-weight: bold;">return</span> <span class="n">root</span>
<a name="line-47" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a><span class="c" style="color: rgb(64, 128, 128); font-style: italic;">###########################################################################</span>
<a name="line-48" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a>
<a name="line-49" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a><span class="n">p</span> <span class="o" style="color: rgb(102, 102, 102);">=</span> <span class="n">ParsedHTML</span><span class="p">(</span><span class="s" style="color: rgb(186, 33, 33);">'file03_01_in.html'</span><span class="p">)</span>
<a name="line-50" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a><span class="nb" style="color: rgb(0, 128, 0);">print</span> <span class="n">p</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">print_tree</span><span class="p">()</span>
<a name="line-51" style="border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(102, 102, 102); color: rgb(51, 51, 51);"></a><span class="nb" style="color: rgb(0, 128, 0);">print</span> <span class="n">p</span><span class="o" style="color: rgb(102, 102, 102);">.</span><span class="n">count</span><span class="p">()</span></pre><pre style="font-family: 'Bitstream Vera Sans Mono', 'Courier New', Monaco, monospace; font-size: 14px; color: rgb(0, 0, 0);"><span class="p"><br></span></pre><pre style="font-family: 'Bitstream Vera Sans Mono', 'Courier New', Monaco, monospace; font-size: 14px; color: rgb(0, 0, 0);">Questo va bene.Il problema è che devo fare la stessa cosa però cambiando al posto di DEF PARSEDHTML metterci una classe PARSEDHTML</pre><pre style="font-family: 'Bitstream Vera Sans Mono', 'Courier New', Monaco, monospace; font-size: 14px; color: rgb(0, 0, 0);">Qualcosa di simile</pre><pre><font color="#000000" face="Bitstream Vera Sans Mono, Courier New, Monaco, monospace"><span style="font-size: 14px;">class ParsedHTML(object):
def __init__(self, htmlfile):
with open(htmlfile, 'U') as f:
root = html.parse(f.read(), HTMLNode)<br></span></font></pre><pre><font color="#000000" face="Bitstream Vera Sans Mono, Courier New, Monaco, monospace"><span style="font-size: 14px;"><br></span></font></pre><pre><font color="#000000" face="Bitstream Vera Sans Mono, Courier New, Monaco, monospace"><span style="font-size: 14px;">L'albero me lo crea ma non so poi come fare il count, in pratica non so come interagirci con l'albero appena creato perche' se faccio p.count mi dice che la mia classe non ha l'attributo count.</span></font></pre><pre><font color="#000000" face="Bitstream Vera Sans Mono, Courier New, Monaco, monospace"><span style="font-size: 14px;">Avevo pensato di fare una def count anche sotto questa classe che mi richiamasse la prima ma non riesco a ricavarmi i dati del primo costruttore per poi lavorarci su. Dove sbaglio? Come posso interagire con l'albero appena creato? Qualcuno può aiutarmi a capire? Grazie mille</span></font></pre></div>