Use the Python script to replace the absolute url with the relative url.
A company project needs to upload images. At first, after uploading images, a colleague combined the current host into an absolute URL (http: // 192.168.1.1: 888/m/getimg? Filename=xxx.jpg: because it provides interfaces for mobile terminals at the same time, some bugs may occur in mobile phone seek. After the code is changed, it is required to replace the previous uri with a relative URL (/m/getimg? Filename=xxx.jpg). Because the image is embedded with the img label and the label is used to enclose the large image, you need to read the database and replace the content,
The script content is as follows:
#!/usr/bin/env python#-*- coding:utf-8 -*-### author : cold night# email : wh_linux@126.com#import pymongoimport refrom StringIO import StringIOconn = pymongo.Connection()db = conn.testdef replace_url(): regex = re.compile(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()