要替换的文件全是html文件,因为文件路径改变,所以需要对html引用的一些文件路径做替换
规则:
src="/js/
href="css/
href="/index
href="/list
替换成
src="/zt/js/
href="zt/css/
href="/zt/index
href="/zt/list
思路:
第一用python查询数据库,把所有需要替换的文件的路径全部获取;
第二根据替换规则,用python调用shell命令做替换操作。
shell命令分析如下:
-----------------------------------------------------------href="css
href=\"\/css\/
替换
href=\"\/zt\/css\/sed -i "s/href=\"\/css\//href=\"\/zt\/css\//g" 22584.html
------------------------------
src="/js/src=\"\/js\/
替换
src=\"\/zt\/js\/sed -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)