在phonebook中獲取郵箱json並把郵箱提取出來的指令碼
阿新 • • 發佈:2021-11-29
在phonebook中獲取郵箱json並把郵箱提取出來的指令碼。
題記
因為護網需要資訊收集,郵箱也是很重要的點,有一次讀到這裡我決定嘗試一下搜一下郵箱試試(這裡以某銀行為例)。
訪問ccb.com響應了賊多郵箱,複製貼上太麻煩了,很難粘。
檢視F12發包過程發現網站查詢的時候發了兩個包,返回郵箱的資料包為json格式,經過考慮,一開始想用正則把郵箱爬出來,後來編寫過後發現json解析更快,於是換種模式。(get新知識:json取值)
把json提取出來
把第二個result響應的所有json複製粘貼出來,儲存為email.json。
用火狐開啟json檔案(可美化)看一下大概樣子,要取哪些值。
我們要在selectors下取selectorvalue值。
核心程式碼
此程式碼為在selectors下取selectorvalue值。
with open('email.json') as f: data = json.load(f) for tag in data["selectors"]: mm=tag["selectorvalue"] print(mm) with open(r'guodu.txt', 'a+', encoding='utf-8') as f: f.write(mm + '\n') f.close()
操作美化與功能完善
因為取值很簡單,又怕寫完以後就徹底不用了,決定完善一下,
1、為區分一下檔案,採用sys取引數與email.txt進行拼接命名。
2、每次啟動清空guodu.txt檔案往裡寫東西。
3、解析完郵箱判斷以前爬的檔名是否存在,存在就刪掉在改名為$host$-email.txt,不存在就直接改名為$host$-email.txt
最終程式碼:
# coding=gbk import json import os import sys from pprint import pprint def result(): with open('email.json') as f: data = json.load(f) for tag in data["selectors"]: mm=tag["selectorvalue"] print(mm) with open(r'guodu.txt', 'a+', encoding='utf-8') as f: f.write(mm + '\n') f.close() if __name__ == '__main__': #判斷是否有引數,沒有退出 if len(sys.argv) != 2 : print ('usage:python test.py host') sys.exit(1) #每次啟動時清空1個txt檔案 if os.path.exists("guodu.txt"): f = open("guodu.txt", 'w') f.truncate() #檔名字為$host$-email.txt filename=sys.argv[1]+"-email.txt" print("最後儲存的檔名為:"+filename) #在json檔案獲取郵箱欄位,儲存在guodu.txt中 result() #判斷檔名是否存在,存在就刪掉在改名為$host$-email.txt,不存在就直接改名為$host$-email.txt if os.path.exists(filename): print(1) os.remove(filename) os.rename('guodu.txt', filename) else: print(2) os.rename('guodu.txt', filename)
操作記錄(用於自己回顧)
python test.py
python test.py ccb(第一次生成這個檔案ccb-email.txt)
python test.py ccb(第二次生成這個檔案ccb-email.txt,因為檔案存在就刪了以前的,在改名)