LoginSignup
0
0

More than 5 years have passed since last update.

lxmlで名前空間付きtagの名前を書き換える

Posted at
from lxml import etree

class ElementsTagReplacer:
    """replace tag name with namespaces."""
    def __init__(self):
        self.encoding = 'utf-8'
        self.parser = etree.XMLParser(encoding=self.encoding)
        self.namespaces = {'media': "http://search.yahoo.com/mrss/"}

    def replace(self, xml):
        if isinstance(xml, str):
            xml = xml.encode(self.encoding, 'ignore')

        root = etree.fromstring(xml, parser=self.parser)

        for analytics_gn in root.iterfind('.//media:thumbnail', namespaces=self.namespaces):
            analytics_gn.tag = "{%s}image" % analytics_gn.nsmap['media']

        return etree.tostring(root, encoding='utf-8').decode('utf-8', 'ignore')


replaced_xml = ElementsTagReplacer().replace(xml)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0