Python HTMLParser无法解析复杂的自定义标记属性

时间:2014-11-07 17:18:33

标签: python html html-parsing custom-tags

我尝试使用HTMLParser来解析以下HTML标记:

<input type="hidden" name="movingEventItemId" value="<dt:value property="movingEventItemId" javaScriptSafe="1"/>"/>

当我获得个别属性时,我希望它返回以下内容:

键入 - 隐藏

name - movingEventItemId

值 - &lt; dt:value property =&#34; movingEventItemId&#34; javaScriptSafe =&#34; 1&#34; /&GT;

但价值又回来了 - &lt; dt:value property =&#34; javascriptsafe =&#34; 1&#34; /&GT;

Python代码:

 quote = '[\\\'\\"]'

 class HiddenInputParser(HTMLParser):
   def handle_starttag(self, tag, attrs):
    if tag == 'input':
        id = self.findAttr('id', attrs)
        name = self.findAttr('name', attrs)
        if not name and not id:
            print('no name or id     ' + self.get_starttag_text())
        elif id is not None and name is not None and id != name:
            print('id != name    ' + self.get_starttag_text())
        else:
            id = (id if id else name)
            output = '<input type="hidden" id="' + id + '" name="' + id + '" '
            for attr in attrs:
                key = attr[0]
                if attr[1] and key != 'id' and key != 'name' and key != 'type':
                    matcher = re.search( key + '\\s*=\\s*(' + quote + ')', self.get_starttag_text(), re.IGNORECASE)
                    quote2 = matcher.group(1)
                    value = quote2 + attr[1] + quote2
                    output += ' ' + key + '=' + value
            output += '/>'
            print ( output )
    else:
        print ( self.get_starttag_text() )

    def findAttr(self, id, attrs):
      for attr in attrs:
          if attr[0] == id:
              return attr[1]


def fixHiddenInputs():
   inputParser = HiddenInputParser()
   files = []
   for extension in extensions:
      for root, dirnames, filenames in os.walk(path):
         for filename in fnmatch.filter(filenames, extension):
            for line in fileinput.input(os.path.join(root, filename), inplace=1):
                line = line.rstrip()
                if ( re.search( '<input.*type=' + quote + 'hidden' + quote,  line ) ):
                    inputParser.feed(line)
                else:
                    print( line)

def convertHiddenInputs():
   pass

convertHiddenInputs()
fixHiddenInputs()

代码是在给定name属性值的情况下将id添加到输入标记。最终结果应如下所示:

<input type="hidden" id="displayOptions" name="displayOptions" value="<dt:value property="movingEventItemId" javaScriptSafe="1"/>"/>

这就是我得到的:

<input type="hidden" id="displayOptions" name="displayOptions"  value="<dt:value property=" javascriptsafe="1"/>

1 个答案:

答案 0 :(得分:0)

要为标记添加id,不需要解析整行并获取每个属性。对于您的问题,更好的解决方案就是将id添加到元素中,尽管其余的属性。如果您需要idname相同,看起来您已经获得了name属性。只需使用您获得的name并在其之前或之后附加id="NAME_VALUE"即可。

干杯!