2

I have the following HTML markup and I'd like to get the English description as plain text out of this snippet - without the "English, and without any tags":

from lxml import etree


html = '''
    <td class="description">
    <p><b>English:</b> Ulm, Germany, old town with Münster, city wall and Metzgerturm, as seen from the south bank of the river Danube.</p>
    <p><b>Deutsch:</b> Ulm, Ansicht der Altstadt vom rechten Donauufer aus. Abgebildet ist das Donauschwabenufer, von der Altstadt sind erkennbar: Das dominante Münster mit Hauptturm, Schiff und zwei Chortürmen; unterhalb des Hauptturms des Münsters zwei Giebel, die zur historischen Krone gehören (linker Giebel größtenteils von Bäumen verdeckt); die Spitze der neuen, gläsernen Stadtbibliothek von Gottfried Böhm; weiter rechts zwei Giebel des Rathauses (gelb-braun); am rechten Rand der Metzgerturm als Teil der Stadtbefestigung.</p>
    </td>
'''

tree = etree.fromstring(html)
for el in tree.xpath('//td[contains(concat(" ", normalize-space(@class), " "), " description ")]/div|p'):
    print etree.tostring(el)

With this script I get the single paragraphs including all tags, but I'm stuck there ... The paragraphs may be DIVs, as well; therefore I've used div|p inside the xpath.

The lxml solution should also work with DIV containers like this:

<td class="description">
    <div class="description mw-content-ltr et" dir="ltr" lang="et" style=""><span class="language et" title=""><b>Eesti:</b></span> Olen loonud selle pildi, kui ma nägin arutelu uue Wiki logo.</div>
    <div class="description mw-content-ltr en" dir="ltr" lang="en" style=""><span class="language en" title=""><b>English:</b></span> "Prototype" for new Wiktionary Logo</div>
</td>
Simon Steinberger
  • 6,605
  • 5
  • 55
  • 97

1 Answers1

4

The text you want to get is tail of b tags:

import lxml.html

html = u'''
    <td class="description">
    <p><b>English:</b> Ulm, Germany, old town with Münster, city wall and Metzgerturm, as seen from the south bank of the river Danube.</p>
    <p><b>Deutsch:</b> Ulm, Ansicht der Altstadt vom rechten Donauufer aus. Abgebildet ist das Donauschwabenufer, von der Altstadt sind erkennbar: Das dominante Münster mit Hauptturm, Schiff und zwei Chortürmen; unterhalb des Hauptturms des Münsters zwei Giebel, die zur historischen Krone gehören (linker Giebel größtenteils von Bäumen verdeckt); die Spitze der neuen, gläsernen Stadtbibliothek von Gottfried Böhm; weiter rechts zwei Giebel des Rathauses (gelb-braun); am rechten Rand der Metzgerturm als Teil der Stadtbefestigung.</p>
    </td>
    <td class="description">
        <div class="description mw-content-ltr et" dir="ltr" lang="et" style=""><span class="language et" title=""><b>Eesti:</b></span> Olen loonud selle pildi, kui ma nägin arutelu uue Wiki logo.</div>
        <div class="description mw-content-ltr en" dir="ltr" lang="en" style=""><span class="language en" title=""><b>English:</b></span> "Prototype" for new Wiktionary Logo</div>
    </td>
'''

tree = lxml.html.fromstring(html)
for el in tree.cssselect('td.description p b, td.description span'):
    if el.text_content().startswith('English'):
        print(el.tail.strip())

output:

Ulm, Germany, old town with Münster, city wall and Metzgerturm, as seen from the south bank of the river Danube.
"Prototype" for new Wiktionary Logo

NOTE You need to use lxml.html. Otherwise, you can't use text_content() method.

Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69
falsetru
  • 357,413
  • 63
  • 732
  • 636