Dict aus HTML mit BeautifulSoup erstellen / Tabelle / br

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
Benutzeravatar
martinjo
User
Beiträge: 186
Registriert: Dienstag 14. Juni 2011, 20:03

Hallo,

da ich öfters im Internet eine Ergebnisse einer bestimmten Suchseite auswerten muss wollte ich mich heute endlich mal an eine Automatisierung machen. Ein guter Zeitpunkt um sich mal BeautifulSoup anzuschauen, dachte ich.

Ich habe es dann auch geschafft den für mich wichtigen Teil zu extrahieren, komme hier allerdings nicht weiter. Ich würde gerne ein Dict mit den Ergebnissen erstellen welches folgendermaßen aussieht:

Code: Alles auswählen

NAME1:100000000
NAME2:200000000
NAME3:300000000
NAME4:400000000
Die HTML Suppe sieht folgendermaßen aus:

Code: Alles auswählen

<span id="lblManu">
    <br />
    <br />
    <b>Information:</b>
    <br />
    <table class="table" border="1" style="border-color:Black;">
        <tr>
            <td>100000000</td>
            <td><a href="/detail/info.aspx?parameter=D1" target="_cage"><font color="blue">D2</font></a></td>
            <td>NAME1</td>
        </tr>
        <tr>
            <td>200000000</td>
            <td><a href="/detail/info.aspx?parameter=D2" target="_cage"><font >D3</font></a></td>
            <td>NAME2</td>
        </tr>
        <tr>
            <td>300000000</td>
            <td><a href="/detail/info.aspx?parameter=D3" target="_cage"><font color="blue">D3</font></a></td>
            <td>NAME3</td>
        </tr>
        <tr>
            <td>400000000</td>
            <td><a href="/detail/info.aspx?parameter=D4" target="_cage"><font color="blue">D4</font></a></td>
            <td>NAME4</td>
        </tr>
    </table>
</span>
Kann mir da jemand weiterhelfen?

Danke
Benutzeravatar
sparrow
User
Beiträge: 4164
Registriert: Freitag 17. April 2009, 10:28

Hilft dir das?

Code: Alles auswählen

from BeautifulSoup import BeautifulSoup

doc = """
<span id="lblManu">
    <br />
    <br />
    <b>Information:</b>
    <br />
    <table class="table" border="1" style="border-color:Black;">
        <tr>
            <td>100000000</td>
            <td><a href="/detail/info.aspx?parameter=D1" target="_cage"><font color="blue">D2</font></a></td>
            <td>NAME1</td>
        </tr>
        <tr>
            <td>200000000</td>
            <td><a href="/detail/info.aspx?parameter=D2" target="_cage"><font >D3</font></a></td>
            <td>NAME2</td>
        </tr>
        <tr>
            <td>300000000</td>
            <td><a href="/detail/info.aspx?parameter=D3" target="_cage"><font color="blue">D3</font></a></td>
            <td>NAME3</td>
        </tr>
        <tr>
            <td>400000000</td>
            <td><a href="/detail/info.aspx?parameter=D4" target="_cage"><font color="blue">D4</font></a></td>
            <td>NAME4</td>
        </tr>
    </table>
</span>
"""

soup = BeautifulSoup(doc)
table = soup.find('table')

for tr in table.findAll('tr'):
    tds = tr.findAll('td')
    print "%s:%s" % (tds[2].string, tds[0].string)
Benutzeravatar
martinjo
User
Beiträge: 186
Registriert: Dienstag 14. Juni 2011, 20:03

Super, vielen Dank!
BlackJack

`find()` und `findAll()` kann man in der Regel durch Attributzugriffe und Aufrufe der Soup-Objekte ersetzen:

Code: Alles auswählen

In [215]: dict((trs[2].string, trs[0].string) for trs in (tr('td') for tr in soup.table('tr')))
Out[215]: 
{u'NAME1': u'100000000',
 u'NAME2': u'200000000',
 u'NAME3': u'300000000',
 u'NAME4': u'400000000'}
Antworten