最近仿站,需要新增栏目和修改的栏目有些多,就想着用Python来实现批量新增和编辑,这里还是对dedecms织梦后台栏目进行的操作。
from urllib.parse import unquote import requests from xpinyin import Pinyin # post数据可以通过fiddler抓包得来 data = 'dopost=save&reid=118&topid=0&moresite=0&siteurl=&sitepath=%7Bcmspath%7D%2Fchtchh&issend=1&ishidden=0&channeltype=3&typename=%E5%81%A5%E8%BA%AB%E8%BF%90%E5%8A%A8&sortrank=0&corank=0&nextdir=%7Bcmspath%7D%2Fchtchh%2F&typedir=jshyund&referpath=parent&isdefault=1&defaultname=index.html&ispart=0&cross=0&crossid=&dfstyle=%7Bstyle%7D&tempindex=%7Bstyle%7D%2Findex_soft.htm&templist=%7Bstyle%7D%2Flist_soft.htm&temparticle=%7Bstyle%7D%2Farticle_soft.htm&namerule=%7Btypedir%7D%2F%7Baid%7D.html&namerule2=%7Btypedir%7D%2Flist_%7Btid%7D_%7Bpage%7D.html&seotitle=%E5%81%A5%E8%BA%AB%E8%BF%90%E5%8A%A8&keywords=%E5%81%A5%E8%BA%AB%E8%BF%90%E5%8A%A8&description=%E5%81%A5%E8%BA%AB%E8%BF%90%E5%8A%A8%E7%B4%A0%E6%9D%90%E5%88%86%E4%BA%AB&content=&imageField.x=35&imageField.y=13'; pdata = {} for line in data.split('&'): # 按照字符:进行划分读取 # 其设置为1就会把字符串拆分成2份 name, value = line.strip().split('=', 1) pdata[name] = unquote(value) # 转换成字典 #需要新增的栏目填写这里 insert = ("字体下载", "前端资源", "PPT模板", "设计工具", ) #headers数据可以通过fiddler抓包得来 headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Host': 'de.80zx.com', 'Pragma': 'no-cache', 'Origin': 'http://0.80zx.com/', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest', 'Cookie': 'menuitems=1_1%2C2_1%2C3_1%2C4_1; UM_distinctid=16c7ff982073b0-08e829aaa3953-79391a30-15f900-16c7ff9820832d; Hm_lvt_061570d255a89355ff0f90bf0e389e0c=1573177823; Hm_lpvt_061570d255a89355ff0f90bf0e389e0c=1573177823; PHPSESSID=8j8gbkfq2766qbikg0n0eb6ch0; DedeUserID=1; DedeUserID__ckMd5=1ef1ea12d7cb101c; adminer_version=4.7.4; DedeLoginTime=1573221152; DedeLoginTime__ckMd5=9a99f1173b143ed9; ENV_GOBACK_URL=%2Fdede%2Fcontent_i_list.php%3Fchannelid%3D3%26cid%3D1; lastCid=16; lastCid__ckMd5=fbe1aa7f1c5bbea5' } p = Pinyin() for item in insert: #这里可以根据自己的环境赋值 pdata['typename'] = item pdata['seotitle'] = item pdata['keywords'] = item pdata['description'] = item + '素材分享' pdata['typedir'] = p.get_initials(item, '').replace('/', '_').lower() print(item) # print(pdata) res = requests.post('http://0.80zx.com/dede/catalog_add.php', data=pdata, headers=headers) if res.content.decode('utf-8').find('成功创建一个分类') >= 0: print('新增栏目%s成功' % pdata['typename']) else: print('新增栏目%s失败' % pdata['typename'])
对应编辑栏目实现的操作方式:
from urllib.parse import unquote import requests from xpinyin import Pinyin pdata={'dopost': 'save', 'id': '73', 'topid': '5', 'issend': '1', 'ishidden': '0', 'channeltype': '3', 'typename': '展示模板', 'sortrank': '0', 'corank': '0', 'typedir': '{cmspath}/wyshj/zhshmb', 'isdefault': '1', 'defaultname': 'index.html', 'ispart': '0', 'cross': '0', 'crossid': '', 'moresite': '0', 'siteurl': '', 'dfstyle': '{style}', 'tempindex': '{style}/index_soft.htm', 'templist': '{style}/list_soft.htm', 'temparticle': '{style}/article_soft.htm', 'namerule': '{typedir}/{aid}.html', 'namerule2': '{typedir}/list_{tid}_{page}.html', 'seotitle': '展示模板', 'keywords': '展示模板', 'description': '展示模板素材分享', 'upnext': '1', 'content': ' ', 'imageField.x': '42', 'imageField.y': '8'} insert={ "12":"办公用品", "64":"服饰展示", "68":"洗化用品" } headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding':'gzip, deflate, sdch', 'Accept-Language':'zh-CN,zh;q=0.8', 'Connection':'keep-alive', 'Cache-Control':'no-cache', 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', 'Host':'de.80zx.com', 'Pragma':'no-cache', 'Origin':'http://0.80zx.com/', 'Upgrade-Insecure-Requests':'1', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', 'X-Requested-With':'XMLHttpRequest', 'Cookie': 'menuitems=1_1%2C2_1%2C3_1%2C4_1; UM_distinctid=16c7ff982073b0-08e829aaa3953-79391a30-15f900-16c7ff9820832d; Hm_lvt_061570d255a89355ff0f90bf0e389e0c=1573177823; Hm_lpvt_061570d255a89355ff0f90bf0e389e0c=1573177823; PHPSESSID=8j8gbkfq2766qbikg0n0eb6ch0; DedeUserID=1; DedeUserID__ckMd5=1ef1ea12d7cb101c; adminer_version=4.7.4; DedeLoginTime=1573221152; DedeLoginTime__ckMd5=9a99f1173b143ed9; ENV_GOBACK_URL=%2Fdede%2Fcontent_i_list.php%3Fchannelid%3D3%26cid%3D1; lastCid=16; lastCid__ckMd5=fbe1aa7f1c5bbea5' } p = Pinyin() for k in insert: pdata['id']=k pdata['typename']=insert[k] pdata['seotitle']=insert[k] pdata['keywords']=insert[k] pdata['description']=insert[k]+'素材分享' pdata['typedir']='{cmspath}/yjmx/'+p.get_initials(insert[k],'').replace('/','_').lower() print(k+':'+insert[k]) # print(pdata) res = requests.post('http://0.80zx.com/dede/catalog_edit.php', data=pdata, headers=headers) if res.content.decode('utf-8').find('成功更改一个分类!')>=0: print('成功更新id为'+k) else: print('保存失败id为' + k)
如果本文对你有帮助,欢迎打赏本站