python 爬虫抓取页面图片

# -*- coding: utf-8 -*-
#  path: D:/Python27/img/jpg.py

import re
import urllib
import os


#获取html页面的内容
def getHtml(url):
    cont = urllib.urlopen(url).read()
    return cont

#获取img标签的url
# String   html   html页面的内容
# String   dir    保存图片的目录,默认为当前目录
def getImg(html, dir):
    reg = r'class="BDE_Image" src="(.*?\.jpg)" size'
    imgre = re.compile(reg)
    imglist = re.findall(imgre, html)
    #目录不存在则创建
    cr_dir(dir)
    x = 0
    #下载图片
    for imgurl in imglist:
        urllib.urlretrieve(imgurl, dir+'\\%s.jpg' % x)
        x += 1
    print 'OK!'

#目录不存在则创建
def cr_dir(dir):
    if not os.path.exists(dir):
        os.makedirs(dir)


#获取html页面内容
url = 'http://tieba.baidu.com/p/5001852004?red_tag=t2336655710'
cont = getHtml(url)

#下载图片
getImg(cont, 'image')





你可能感兴趣的:(Python2.x)