首页 > 其他 > 详细

下载图片-使用正则表达式

时间:2015-08-02 18:19:18      阅读:175      评论:0      收藏:0      [点我收藏+]

图片处理


如何处理图片

  1. 拿到网页
  2. 使用正则表达式匹配
  3. 使用urlretrieve下载图片
import re
import urllib2
import urllib

def getContext(url):
    ‘‘‘
    获取html
    ‘‘‘
    html = urllib2.urlopen(url)

    return html.read()

def getPicture(html):
    ‘‘‘
    获取图片网址
    ‘‘‘
    regex = r‘class="BDE_Image" src="(.+?\.jpg)"‘

    pattern = re.compile(regex)

    imageUrl = re.findall(pattern, html)

    i = 1
    for url in imageUrl:

        print url

        urllib.urlretrieve(url, ‘%s.jpg‘ % i)

        i +=1

url = ‘http://tieba.baidu.com/p/3932177087‘        
if __name__==‘__main__‘:
    html = getContext(url)

    getPicture(html)

版权声明:本文为博主原创文章,未经博主允许不得转载。

下载图片-使用正则表达式

原文:http://blog.csdn.net/weiyongxuan/article/details/47208699

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!