用Python将绝对URL替换成相对URL的代码
下面的内容内容是关于用Python将绝对URL替换成相对URL的内容,应该是对码农有些用途。
#!/usr/bin/env python
#
#
# 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():
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()