Pythonフォームデータのコミット
6.3フォームデータの提出6.3.1 GET方法
6.3.2 POST方法
import sys
import urllib2
import urllib
# URL
def addGETdata(url, data):
return url + '?' + urllib.urlencode(data)
#
zipcode = sys.argv[1]
url = addGETdata('http://www.wunderground.com/cig-bin/findweather/getForecast', [('query', zipcode)])
print 'Using URL', url
req = urllib2.Request(url)
# Get
fd = urllib2.urlopen(req)
while True:
data = fd.read(1024)
if not len(data):
break
sys.stdout.write(data)
6.3.2 POST方法
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)
# POST
fd = urllib2.urlopen(req, data)
while True:
data = fd.read(1024)
if not len(data):
break
sys.stdout.write(data)