The example in this article describes how the Python implementation replaces an absolute URL with a relative URL. Share to everyone for your reference. The specific analysis is as follows:
First, the question:
A project needs to upload pictures, the beginning of a colleague will upload pictures after the combination of the current host to spell an absolute URL (http://192.168.1.1:888/m/getimg?filename=xxx.jpg) because at the same time to the phone terminal to provide interface, At the end of the mobile phone will cause some bugs, after the change of code requirements to replace the previous URI to the relative URL (/m/getimg?filename=xxx.jpg), because the image is embedded into the content with an IMG tag and a label to show the larger picture, So you need to read the database and replace the content.
Second, the solution:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
#!/usr/bin/env python #-*-coding:utf-8 # # # Author:cold Night # email:wh_linux@126.com # import Pymongo import re from stringio import Stringio conn = Pymon Go. Connection () db = Conn.test def 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 () To 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 () |
I hope this article will help you with your Python programming.