python执行get提交的方法

yipeiwu_com5年前Python基础

本文实例讲述了python执行get提交的方法。分享给大家供大家参考。具体如下:

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 = 'S2S 7U8'
url = addGETdata('http://www.yoursiteweb.com/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)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

深入解析Python中的list列表及其切片和迭代操作

有序列表list >>> listTest = ['ha','test','yes'] >>> listTest ['ha', 'test', '...

实例详解Python装饰器与闭包

闭包是Python装饰器的基础。要理解闭包,先要了解Python中的变量作用域规则。 变量作用域规则 首先,在函数中是能访问全局变量的: >>> a = 'glob...

Python中asyncore的用法实例

本文实例讲述了python中asyncore模块的用法,分享给大家供大家参考。具体方法如下: 实例代码如下: ##asyncore import asyncore,socket...

Python with关键字,上下文管理器,@contextmanager文件操作示例

本文实例讲述了Python with关键字,上下文管理器,@contextmanager文件操作。分享给大家供大家参考,具体如下: demo.py(with 打开文件): # ope...

tensorflow: 查看 tensor详细数值方法

问题 tensor详细数值 不能直接print打印: import tensorflow as tf x = tf.constant(1) print x 输出: Tensor...