def pattern_extract_forms(tree, nodeclass, nodeid, fields):
    """Extracts web forms from page"""
    res = []
    formattrlist = ['name', 'id', 'action', 'class', 'method']
    inputattrlist = ['name', 'id', 'type', 'class', 'value', 'src', 'size']
    textarealist = ['name', 'id', 'size', 'class']
    buttonlist = ['name', 'id', 'value', 'class']
    selectlist = ['name', 'id', 'multiple', 'size', 'class']
    optionlist = ['value', 'selected', 'class']
    tagnames = [('input', inputattrlist), ('textarea', textarealist), (
        'button', buttonlist), ('select', selectlist)]
    allforms = tree.xpath('//form')
    for form in allforms:
        fkey = {}
        for k in formattrlist:
            if form.attrib.has_key(k):
                fkey[k] = form.attrib[k]
        for tag in form.iterdescendants():
            if not hasattr(tag, 'tag'):
                continue
            for tagname, tlist in tagnames:
                if tag.tag == tagname:
                    if not tagname in fkey.keys():
                        fkey[tagname] = []
                    tval = {'text': tag.text}
                    for k in tlist:
                        if tag.attrib.has_key(k):
                            tval[k] = tag.attrib[k]
                    if tag.tag == 'select':
                        tval['options'] = []
                        options = tag.xpath('option')
                        for o in options:
                            optionval = {'text': o.text}
                            for k in optionlist:
                                if o.attrib.has_key(k):
                                    optionval[k] = o.attrib[k]
                            tval['options'].append(optionval)
                    fkey[tagname].append(tval)
        res.append(fkey)
    return {'total': len(res), 'list': res}
