Is Python powerful enough for this?
Roman Suzi
rnd at onego.ru
Fri Aug 31 06:38:39 EDT 2001
On Fri, 31 Aug 2001, A wrote:
> On 30 Aug 2001, at 17:36, Roman Suzi wrote:
>
> > On Thu, 30 Aug 2001, A wrote:
> >
> > > Hi,
> > > I have some webpages that contain FORMS. Each form has input
> > > fields and also some hidden fields.
> > > How can I easily get urlencoded string from all these fields
> > > like
> > > Key1=Value1&Key2=Value2&Key3=Value3
> > >
> > > For example:
> > > I have a following web page
> > > <html>
> > >
> > > <head>
> > > </head>
> > > <body>
> > > <form method="POST" action="c.cgi">
> > > <p><input type="text" name="T1" size="20"></p>
> > > <p><input type="checkbox" name="C1" value="ON"></p>
> > > <p><textarea rows="2" name="S1" cols="20"></textarea></p>
> > > <p><input type="submit" value="Submit" name="B1"><input
> > > type="reset" value="Reset" name="B2"></p>
> > > </form>
> > > </body>
> > > </html>
> > >
> > > >From the above
> > > I would like to receive
> > > T1=&S1=&C1=&B1=Submit
> >
> > I do not follow what are you trying to do?
> >
> > Do you need to convert HTML input into corresponding urlencoded
> > output as if the form were filled empty?
> Yes.
> >
> > For this you can make your htmllib.HTMLParser and make handlers for
> > form-specific tags. (In fact, sgmllib.SGMLParser is even easier to use for
> > this task).
> Can you please give an example? I am a newbie with Python
import sgmllib, urllib
example = """ <form><input name="aaa" value="bbb" </form>"""
class FormParser(sgmllib.SGMLParser):
def __init__(self):
sgmllib.SGMLParser.__init__(self)
self._record = 0
self._form = {}
def __str__(self):
return urllib.urlencode(self._form)
def start_form(self, atts):
attdict = self._attdict(atts)
self._record = 1
def end_form(self):
self._record = 0
def _attdict(self, atts):
attdict = {}
for attname, attval in atts:
attdict[attname] = attval
return attdict
def start_input(self, atts):
if self._record:
attdict = self._attdict(atts)
if attdict.has_key("name"):
name = attdict["name"]
if attdict.has_key("value"):
self._form[name] = attdict["value"]
else:
self._form[name] = ""
start_select = start_textarea = start_input
f = FormParser()
f.feed(example)
print str(f)
> > And what if there are several forms on the page?
> There should be only one.
>
> >
> Thank you for help.
> Ladislav
Sincerely yours, Roman A.Suzi
--
- Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
More information about the Python-list
mailing list