圆月山庄资源网 Design By www.vgjia.com

公司一个项目需要上传图片,一开始同事将图片上传后结合当前主机拼成了一个绝对的URL(http://192.168.1.1:888/m/getimg"htmlcode">

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#
#
# author : cold night
# email  : wh_linux@126.com
#

import pymongo
import re
from StringIO import StringIO

conn = pymongo.Connection()
db = conn.test

def replace_url():
  regex = re.compile(r'([href|src])=["|\']http://.*"|\']')
  results = db['test'].find()
  db_coll = db['test']
  def replace(r):
    content = r.get('content')
    if not content: return
    content = StringIO(content)
    content.seek(0)
    result = StringIO()
    for line in content.readlines():
      t = regex.sub(r'\1="\2"', line)
      result.write(t)

    result.seek(0)
    content = result.read()
    if content:
      r['content'] = content
    _id = r.get('_id')
    db_coll.update({'_id':_id}, r)

  results = [replace(i) for i in results]

if __name__=="__main__":replace_url()

标签:
Python

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com