Changeset 15595 for main/waeup.kofa/trunk/src/waeup
- Timestamp:
- 19 Sep 2019, 23:38:52 (5 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/utils
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py
r14939 r15595 30 30 from cStringIO import StringIO 31 31 from docutils.core import publish_string 32 from HTMLParser import HTMLParser 32 33 from zope.component import getUtility 33 34 from zope.component.interfaces import IFactory … … 864 865 contents=ReST2HTML(text)) 865 866 return elements 867 868 869 870 class FormVarParser(HTMLParser): 871 """An HTML form parser that extracts keys and values. 872 873 Fed with an HTML document, we parse all starttags and check for each, 874 whether it provides a `name` and a `value` attribute. If so, the 875 values of the respective attributes are stored in instance var 876 `form_vars` as a dict entry. 877 """ 878 879 def __init__(self): 880 HTMLParser.__init__(self) # old-style class - no super() 881 self.form_vars = {} 882 883 def handle_starttag(self, tag, attrs): 884 tag_attrs = {} 885 for key, val in attrs: 886 tag_attrs[key] = val 887 if 'name' in tag_attrs and 'value' in tag_attrs: 888 self.form_vars[tag_attrs['name']] = tag_attrs['value'] 889 890 891 def extract_formvars(html_code): 892 """Extract keys and values from an HTML form as dict. 893 894 No text, no values:: 895 896 >>> extract_formvars("") 897 {} 898 899 Simple input tags normally provide name and value:: 900 901 >>> extract_formvars("<input type='text' name='foo' value='bar'>") 902 {'foo': 'bar'} 903 904 The sample doc we stored in tests is a bit more difficult:: 905 906 >>> html_path = os.path.join(os.path.dirname(__file__), 907 ... 'tests', 'sample_response.html') 908 >>> html_code = open(html_path, 'r').read() 909 >>> import pprint 910 >>> pprint.pprint(extract_formvars(html_code)) 911 {'AMOUNT': '100', 912 ... 913 'TRANS_NUM': '01ESA20190916134824YA3YJ8'} 914 915 """ 916 result = {} 917 parser = FormVarParser() 918 parser.feed(html_code) 919 return parser.form_vars
Note: See TracChangeset for help on using the changeset viewer.