Stefan Behnel schrieb:

>> So I need to build hyperlinks (a elements) with href attribute and
>> replace the text elements (numbers) somehow.
> 
> Try lxml.html instead. It makes it really easy to do these things. For
> example, you can use XPath to find all table cells that contain numbers:
> 
>       td_list = doc.xpath("//td[number() >= 0]")
> 
> or maybe using regular expressions to make sure it's an int:
> 
>       td_list = doc.xpath("//td[re:match(., '^[0-9]+$')]",
>               namespaces={'re':'http://exslt.org/regular-expressions'})
> 
> and then replace them by a hyperlink:
> 
>       # assuming links = ['http://...', ...]
> 
>       from lxml.html.builder import A
>       for td in td_list:
>               index = int(td.text)
>               a = A("some text", href=links[index])
>               td.getparent().replace(td, a)

Oh no! I was looking for something like this for *ages* but always
fought with minidom - where this is a real pain :-(

Had I only known before that such a wonderful library exists. I'll
definitely use lxml from now on. Does it compile with Python3?

Kind regards,
Johannes

-- 
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
         -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
                         <48d8bf1d$0$7510$54022...@news.sunrise.ch>
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to