使用Python的urllib2模块处理url和图片的技巧两则
发布时间:2020-12-16 21:18:29 所属栏目:Python 来源:网络整理
导读:获取带有中文参数的url内容 对于中文的参数如果不进行编码的话,python的urllib2直接处理会报错,我们可以先将中文转换成utf- 8编码,然后使用urllib2.quote方法对参数进行url编码后传递。 content = u'你好 sharejs.com'content = content.encode('utf-8')c
获取带有中文参数的url内容 content = u'你好 sharejs.com' content = content.encode('utf-8') content = urllib2.quote(content) api_url = 'http://www.sharejs.com/q=%s'%content res = urllib2.urlopen(api_url)
#!/usr/bin/env python #encoding=utf-8 import cStringIO,urllib2,Image url = 'http://www.01happy.com/wp-content/uploads/2012/09/bg.png' file = urllib2.urlopen(url) tmpIm = cStringIO.StringIO(file.read()) im = Image.open(tmpIm) print im.format,im.size,im.mode (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |