返回顶部

在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,因为文件存在就删了以前的,在改名)

posted @ 2021-11-29 18:28  11阳光  阅读(221)  评论(0编辑  收藏  举报