清理es索引
阿新 • • 發佈:2021-10-27
依賴安裝
pip install requests-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
如果安裝失敗,可能是Python直譯器版本太高了,可以解除安裝掉,使用pycharm安裝Python直譯器
from requests_html import HTMLSession import json import re # 請求頭 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36', 'Content-Type': 'application/json', 'Cookie': 'PLAY_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYWRtaW4ifSwibmJmIjoxNjM1MzAyNzgwLCJpYXQiOjE2MzUzMDI3ODB9.49ngzEl44NPWD1Hrgd94_D6pvLg8cHpPl7HneYay8iA' } # 請求url url = "http://10.10.1.100:19000/cluster_changes" s = HTMLSession() login_data = {"host": "http://10.10.1.100:9200"} # cookie = { # "Cookie": "PLAY_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYWRtaW4ifSwibmJmIjoxNjM1MzAyNzgwLCJpYXQiOjE2MzUzMDI3ODB9.49ngzEl44NPWD1Hrgd94_D6pvLg8cHpPl7HneYay8iA" # } # 無格式要求直接傳 login_data # cookie寫在post引數裡面不生效 #res = s.post(url=url, headers=headers, cookies=cookie, data=json.dumps(login_data)) res = s.post(url=url, headers=headers, data=json.dumps(login_data)) # 渲染出一個Element物件的HTML內容 #print(res.html.html) # 獲取一個Element物件內的文字內容 #print(res.html.text) # 把json轉換為字典 res_dict = json.loads(res.html.html) #print(type(res_dict)) list = res_dict['body']['indices'] #print(type(list)) url_del = "http://10.10.1.100:19000/overview/delete_indices" #data_del = {"indices":"%{[app]}-2021.10.20","host":"http://10.10.1.100:9200"} for i in range(len(list)-1): #print(list[i]) # 使用正則匹配 pattern = re.compile(r'leliven-lecent-.*-2021.10.2\d') #pattern = re.compile(r'leliven-lecent-.*-2021.10.2\d') result = pattern.findall(list[i]) if len(result) != 0: #print(result) data_del = {"indices": result[0], "host": "http://10.10.1.100:9200"} #print(data_del) # 傳送請求刪除索引 #s.post(url=url_del,headers=headers,data=json.dumps(data_del))