HTML로 실습

샘플 코드 : http://goo.gl/zhKvcI

<html>
    <head>
        <title>Example page</title>
    </head>
    <body>
        <p>Moved to <a href="http://example.org/">example.org</a>
        or <a href="http://example.com/">example.com</a>.</p>
    </body>
</html>

실습

>>> from xml.etree.ElementTree import ElementTree

>>> tree = ElementTree()

>>> tree.parse("index.xhtml")
<Element 'html' at 0xb77e6fac>

>>> p = tree.find("body/p")     # Finds first occurrence of tag p in body

>>> p
<Element 'p' at 0xb77ec26c>

>>> links = list(p.iter("a"))   # Returns list of all links

>>> links
[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]

>>> for i in links:             # Iterates through all found links
...     i.attrib["target"] = "blank"

>>> tree.write("output.xhtml")

results matching ""

    No results matching ""