The Python programming language is a powerful object-oriented language that helps us easily implement many functional requirements. Here, we can first master the related operation methods for submitting forms in Python, hoping to help you in practical application.
Python form submission code example:
- #-*-Coding: cp936 -*-
- Import urllib2, urllib, sys
- Http://www.baidu.com/s"
- Search = [('w', 'python'), ('cl', '3')]
- GetString = url + "? "+ Urllib. urlencode (search)
- Req = urllib2.Request (getString)
- Fd = urllib2.urlopen (req)
- While 1:
- Data = fd. read (1, 1024)
- If not len (data ):
- Break
- Sys. stdout. write (data)
- *************************************
- #-*-Coding: cp936 -*-
- Import urllib2, urllib, sys
- """
- Use POST to submit Form data
- 1. encoding or using urlencode
- 2. Do not use string connection
- 3. Use the data parameter of urlopen
- The example cannot run because www.google.com only supports the GET method and does not provide the POST method.
- """
- Url = "http://www.google.com/search"
- Search = urllib. urlencode ([('Q', 'python')])
- Req = urllib2.Request (url)
- Fd = urllib2.urlopen (req, search)
- While 1:
- Data = fd. read (1, 1024)
- If not len (data ):
- Break
- Sys. stdout. write (data)
The above describes how to submit a Python form.