def lazy(html, src_field='data-lazyload', class_name='lazy'): u''' ''' placeholder_src = 'http://static.fuwo.com/static/images/new/common/placeholder.jpg' soup = BeautifulSoup(html) img_tags = soup.findAll('img') if img_tags: for tag in img_tags: if tag.has_key('src'): tag['class'] = class_name tag[src_field] = tag['src'] tag['src'] = placeholder_src lazy_html = unicode(soup) else: lazy_html = html soup.clear() del soup return lazy_html