脚本内容如下:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#
#
# author : cold night
# email : wh_linux@126测试数据
#
import pymongo
import re
from StringIO import StringIO
conn = pymongo.Connection()
db = conn.test
def replace_url():
regex = re测试数据pile(r'([href|src])=["|\']http://.*?(/m/getimg\?.*?)["|\']')
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脚本将绝对url替换为相对url的教程的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did88535