geany
python 2
I en kursusrække er jeg kommet til en opgave, som jeg ikke kan finde svaret på. Jeg har spurgt på kursets forum. Forummet virker efter min mening ikke hensigtsmæssigt. Når jeg paster den kode, jeg har skrevet og som ikke virker, bliver den slettet. En anden kursusdeltager svarede på et spørgsmål fra mig. Det blev også delvis slettet. Derfor spørger jeg her. Man skal hente data fra en url. Når man har hentet data'ene, skal man extract numbers og summere dem. Jeg kan hente data'ene, men ikke extract dem og summere dem. Man underviser med videoer og denne bog. http://do1.dr-chuck.com/py4inf/EN-us/book.pdf. Opgaven hører til kap 12. Der er blevet undervist op til kap 12.
Den her kode har jeg skrevet.
Kode: Vælg alt
import re
import urllib
from BeautifulSoup import *
url = 'http://python-data.dr-chuck.net/comments_42.html'
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
tags = soup('tr')
print tags
Når koden startes i geany, fremkommer dette i command line.
Kode: Vælg alt
[<tr>
<td>Name</td><td>Comments</td>
</tr>, <tr><td>Leven</td><td><span class="comments">100</span></td></tr>, <tr><td>Mahdiya</td><td><span class="comments">97</span></td></tr>, <tr><td>Ajayraj</td><td><span class="comments">87</span></td></tr>, <tr><td>Lillian</td><td><span class="comments">86</span></td></tr>, <tr><td>Aon</td><td><span class="comments">86</span></td></tr>, <tr><td>Ruaraidh</td><td><span class="comments">78</span></td></tr>, <tr><td>Gursees</td><td><span class="comments">75</span></td></tr>, <tr><td>Emmanuel</td><td><span class="comments">74</span></td></tr>, <tr><td>Christy</td><td><span class="comments">72</span></td></tr>, <tr><td>Annoushka</td><td><span class="comments">72</span></td></tr>, <tr><td>Inara</td><td><span class="comments">72</span></td></tr>, <tr><td>Caite</td><td><span class="comments">70</span></td></tr>, <tr><td>Rosangel</td><td><span class="comments">70</span></td></tr>, <tr><td>Iana</td><td><span class="comments">66</span></td></tr>, <tr><td>Anise</td><td><span class="comments">66</span></td></tr>, <tr><td>Jaosha</td><td><span class="comments">65</span></td></tr>, <tr><td>Cadyn</td><td><span class="comments">65</span></td></tr>, <tr><td>Edward</td><td><span class="comments">63</span></td></tr>, <tr><td>Charlotte</td><td><span class="comments">61</span></td></tr>, <tr><td>Sammy</td><td><span class="comments">60</span></td></tr>, <tr><td>Zarran</td><td><span class="comments">60</span></td></tr>, <tr><td>Rowen</td><td><span class="comments">59</span></td></tr>, <tr><td>Stanislaw</td><td><span class="comments">59</span></td></tr>, <tr><td>Maighdlin</td><td><span class="comments">57</span></td></tr>, <tr><td>Connan</td><td><span class="comments">56</span></td></tr>, <tr><td>Warrick</td><td><span class="comments">54</span></td></tr>, <tr><td>Diya</td><td><span class="comments">52</span></td></tr>, <tr><td>Lawson</td><td><span class="comments">52</span></td></tr>, <tr><td>Wu</td><td><span class="comments">51</span></td></tr>, <tr><td>Irmak</td><td><span class="comments">47</span></td></tr>, <tr><td>Emilija</td><td><span class="comments">47</span></td></tr>, <tr><td>Kayda</td><td><span class="comments">41</span></td></tr>, <tr><td>Ellenor</td><td><span class="comments">41</span></td></tr>, <tr><td>Kyra</td><td><span class="comments">41</span></td></tr>, <tr><td>Nikita</td><td><span class="comments">38</span></td></tr>, <tr><td>Kaelah</td><td><span class="comments">35</span></td></tr>, <tr><td>Meko</td><td><span class="comments">32</span></td></tr>, <tr><td>Marissa</td><td><span class="comments">31</span></td></tr>, <tr><td>Ayat</td><td><span class="comments">24</span></td></tr>, <tr><td>Sali</td><td><span class="comments">19</span></td></tr>, <tr><td>Hashem</td><td><span class="comments">19</span></td></tr>, <tr><td>Tygan</td><td><span class="comments">18</span></td></tr>, <tr><td>Rioden</td><td><span class="comments">17</span></td></tr>, <tr><td>Cruiz</td><td><span class="comments">16</span></td></tr>, <tr><td>Caoilfinn</td><td><span class="comments">13</span></td></tr>, <tr><td>Ewen</td><td><span class="comments">8</span></td></tr>, <tr><td>Baighley</td><td><span class="comments">7</span></td></tr>, <tr><td>Ramone</td><td><span class="comments">1</span></td></tr>, <tr><td>Kyran</td><td><span class="comments">1</span></td></tr>, <tr><td>Noelani</td><td><span class="comments">1</span></td></tr>]
Hvordan extract jeg numbers? Er det ikke en string?