pythonWeb编程提交表单方法

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)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值