獲取帶有中文參數(shù)的url內(nèi)容
對于中文的參數(shù)如果不進(jìn)行編碼的話,python的urllib2直接處理會報(bào)錯(cuò),我們可以先將中文轉(zhuǎn)換成utf- 8編碼,然后使用urllib2.quote方法對參數(shù)進(jìn)行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)
獲取遠(yuǎn)程圖片的大小和尺寸
這段代碼通過urllib2打開遠(yuǎn)程圖片,通過cStringIO讀取文件內(nèi)容,不用保存到磁盤即可讀取圖片文件的信息
#!/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
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

