您的位置:首页 > 编程语言 > Python开发

pythonWeb编程提交表单方法

2015-09-29 10:14 676 查看
Get方法:

'''
Created on 2015-9-29

@author: Administrator
'''
#/usr/bin/env python
#Submit GET Data - Chapter 6 - submit_get.py
import sys, urllib2, urllib

def addGETdata(url, data):
"""
Adds data to url. Data should be a list or tuple consisting of 2-item
lists or tuples of the form: (key, value).

Items that have no key should have key set to None

A given key may occur more than once.
"""
return url + '?' + urllib.urlencode(data)

zipcode = sys.argv[1]
url = addGETdata('http://www.wunderground.com/cgi-bin/findweather/getForecast',
[('query', zipcode)])
print "Using URL", url
req = urllib2.Request(url)
fd = urllib2.urlopen(req)
while 1:
data = fd.read(1024)
if not len(data):
break
sys.stdout.write(data)

Post方法:

'''
Created on 2015-9-29

@author: Administrator
'''
#/usr/bin/env python
#Submit POST Data - Chapter 6 - submit_post.py
import sys, urllib2, urllib

zipcode = sys.argv[1]
url = 'http://www.wunderground.com/cgi-bin/findweather/getForecast'
data = urllib.urlencode([('query', zipcode)])
req = urllib2.Request(url)
fd = urllib2.urlopen(req, data)
while 1:
data = fd.read(1024)
if not len(data):
break
sys.stdout.write(data)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: