要替換的文件全是html文件,由於文件路徑改變,因此須要對html引用的一些文件路徑作替換css
規則:
src="/js/
href="css/
href="/index
href="/list
替換成
src="/zt/js/
href="zt/css/
href="/zt/index
href="/zt/list html
思路:python
第一用python查詢數據庫,把全部須要替換的文件的路徑所有獲取;mysql
第二根據替換規則,用python調用shell命令作替換操做。sql
shell命令分析以下:
---------------------------------------------------------shell--href="css數據庫
href=\"\/css\/
替換
href=\"\/zt\/css\/fetchsed -i "s/href=\"\/css\//href=\"\/zt\/css\//g" 22584.html
------------------------------
src="/js/codesrc=\"\/js\/
替換
src=\"\/zt\/js\/htmsed -i "s/src=\"\/js\//src=\"\/zt\/js\//g" 22584.html
------------------------------
--href="/indexhref=\"\/index
替換
href=\"\/zt\/indexsed -i "s/href=\"\/index/href=\"\/zt\/index/g" 22584.html
------------------------------
href="/listhref=\"\/list
替換
href=\"\/zt\/listsed -i "s/href=\"\/list/href=\"\/zt\/list/g" 22584.html
具體腳本實現:
#!python # -*- coding: UTF-8 -*- import MySQLdb import os dbip = "192.168.0.0" dbuser = "mysql" dbpwd = r"000000" dbdata = "zt" # 打開數據庫鏈接 db = MySQLdb.connect(dbip,dbuser,dbpwd,dbdata ) # 使用cursor()方法獲取操做遊標 cursor = db.cursor() try: # 執行sql語句 sqls="SELECT a.zsid , a.uri_path , a.www_uri_path , a.inum from article_pathzsq a where a.isflag=0 LIMIT 2;" cursor.execute(sqls) results = cursor.fetchall() except: print("讀取失敗") # 關閉數據庫鏈接 db.close() for row in results: zsid = row[0] uri_path = row[1] www_uri_path = row[2] inum = row[3] # 打印結果 """ print(" zsid= %d , uri_path= %s ,www_uri_path= %s ,inum= %s " % \ (zsid, uri_path, www_uri_path, inum)) """ uri_css = 'sed -i "s/href=\\"\/css\//href=\\"\/zt\/css\//g" ' + uri_path+inum uri_js = 'sed -i "s/src=\\"\/js\//src=\\"\/zt\/js\//g" ' + uri_path+inum uri_index = 'sed -i "s/href=\\"\/index/href=\\"\/zt\/index/g" ' + uri_path+inum uri_list = 'sed -i "s/href=\\"\/list/href=\\"\/zt\/list/g" ' + uri_path+inum wwwuri_css = 'sed -i "s/href=\\"\/css\//href=\\"\/zt\/css\//g" ' + www_uri_path+inum wwwuri_js = 'sed -i "s/src=\\"\/js\//src=\\"\/zt\/js\//g" ' + www_uri_path+inum wwwuri_index = 'sed -i "s/href=\\"\/index/href=\\"\/zt\/index/g" ' + www_uri_path+inum wwwuri_list = 'sed -i "s/href=\\"\/list/href=\\"\/zt\/list/g" ' + www_uri_path+inum print(uri_css) print(uri_js) print(uri_index) print(uri_list) os.system(uri_css) os.system(uri_js) os.system(uri_index) os.system(uri_list) print(wwwuri_css) print(wwwuri_js) print(wwwuri_index) print(wwwuri_list) os.system(wwwuri_css) os.system(wwwuri_js) os.system(wwwuri_index) os.system(wwwuri_list)