使用Python的urllib2模块处理url和图片的技巧两则
获取带有中文参数的url内容
对于中文的参数如果不进行编码的话,python的urllib2直接处理会报错,我们可以先将中文转换成utf- 8编码,然后使用urllib2.quote方法对参数进行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)
获取远程图片的大小和尺寸
这段代码通过urllib2打开远程图片,通过cStringIO读取文件内容,不用保存到磁盘即可读取图片文件的信息
#!/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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

About Pythonasyncio...

Using python in Linux terminal...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...
