爬虫项目:高效ChromeDriver驱动的爱站信息精准获取与利用工具
字数 1570 2025-08-22 12:23:12

爱站信息爬取工具开发教学文档

1. 项目概述

本项目是一个基于ChromeDriver的爱站(aizhan.com)信息爬取工具,主要用于批量查询域名的权重信息。该工具能够有效应对爱站的反爬机制,通过多线程技术提高爬取效率,适用于网络安全领域的漏洞挖掘和资产信息收集工作。

2. 核心功能

  • 批量查询域名在爱站的权重信息
  • 获取ICP备案信息和企业名称
  • 支持百度权重、移动权重、360权重、神马权重、搜狗权重和谷歌PR查询
  • 多线程并发处理提高效率
  • 自动处理主域名提取和去重
  • 友好的进度显示和结果输出

3. 技术实现

3.1 主要技术栈

  • Python 3.x
  • Selenium WebDriver
  • ChromeDriver
  • 多线程(concurrent.futures)
  • XPath数据提取
  • 正则表达式处理

3.2 核心模块设计

3.2.1 文件读取模块(file_Read)

def file_Read(path):
    try:
        datas = []
        with open(path, 'r', encoding='utf-8') as file:
            while True:
                data = file.readline()
                if not data:
                    break
                else:
                    datas.append(data)
        return datas
    except Exception as e:
        error(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), '文件读取出错,请检查传入路径及文件类型!')
        info(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), '程序已退出')
        sys.exit()

3.2.2 数据处理模块(data_Processing)

def data_Processing(datas):
    domains_one = []
    for data in tqdm(datas, desc="数据初始化"):
        try:
            domain_name = tldextract.extract(data)
            domain_name = f"{domain_name.domain}.{domain_name.suffix}"
            domains_one.append(domain_name)
        except Exception as e:
            continue
    
    ip_pattern = re.compile(
        r'(?:\d{1,3}\.){3}\d{1,3}'  # IPv4
        r'|(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}'  # IPv6
        r'|(?:[A-Fa-f0-9]{1,4}:){1,7}:?'  # 压缩表示的IPv6
    )
    
    domains = [item for item in domains_one if not ip_pattern.search(item)]
    return domains

3.2.3 多线程处理模块(Multi_threading)

def Multi_threading(domains):
    try:
        with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
            with tqdm(total=len(domains), desc="域名权重查询中") as progress_bar:
                futures = [executor.submit(Weight_query, domain) for domain in domains]
                for future in concurrent.futures.as_completed(futures):
                    progress_bar.update(1)
    except Exception as e:
        pass

3.2.4 权重查询模块(Weight_query和Weight_query_B)

def Weight_query(domain):
    try:
        target = 'https://www.aizhan.com/cha/' + domain
        service = Service(r'./chromedriver.exe', service_log_path=logFilename)
        driver = webdriver.Chrome(service=service, options=chrome_options)
        driver.get(target)
        WebDriverWait(driver, 10)
        page_source = driver.page_source
        driver.quit()
        
        tree = etree.HTML(page_source)
        baidu = tree.xpath('//*[@id="baidurank_br"]/img/@alt')[0]
        yidong = tree.xpath('//*[@id="baidurank_mbr"]/img/@alt')[0]
        three60 = tree.xpath('//*[@id="360_pr"]/img/@alt')[0]
        SM = tree.xpath('//*[@id="sm_pr"]/img/@alt')[0]
        sogou = tree.xpath('//*[@id="sogou_pr"]/img/@alt')[0]
        google = tree.xpath('//*[@id="google_pr"]/img/@alt')[0]
        ICP_ICP = tree.xpath('//*[@id="icp_icp"]')[0]
        ICP_company = tree.xpath('//*[@id="icp_company"]')[0]
        
        line_to_write = f"{domain},{ICP_ICP.text},{ICP_company.text},{baidu},{yidong},{three60},{SM},{sogou},{google}\n"
        line_to_write = line_to_write.replace('\n', '')
        line_to_write = line_to_write + "\n"
        Query_results(line_to_write)
    except Exception as e:
        Weight_query_B(domain)

3.2.5 数据写入模块(Data_writing)

def Data_writing(Weight_data):
    try:
        filename = formatted_now + '.csv'
        with open(filename, 'a', encoding='utf-8') as file:
            file.write("domain_name,ICP,ICP_company,Baidu_weight,yidong_weight,360_weight,SM_weight,sogou_weight,google_weight\n")
            for line in tqdm(Weight_data, desc=f"正在将结果写入文件: {filename}"):
                file.write(line)
        info(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), f'数据写入完成,请查看文件: {filename}')
    except Exception as e:
        error(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), '结果写入出错了!')

3.3 ChromeDriver配置优化

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')  # 无头模式
chrome_options.add_argument('--no-sandbox')  # Linux下去除沙箱模式
chrome_options.add_argument('--log-level=3')  # 减少日志输出
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])  # 禁用日志

4. 使用说明

4.1 安装依赖

pip install selenium tqdm concurrent-log-handler tldextract fake-useragent rich

4.2 下载ChromeDriver

根据本地Chrome浏览器版本下载对应的ChromeDriver,并放在项目目录下。

4.3 命令行参数

  • 单域名查询:

    python aizhan_spider.py -u example.com
    
  • 批量查询(从文件):

    python aizhan_spider.py -f domains.txt
    

4.4 输入文件格式

domains.txt示例:

example.com
sub.example.com
anothersite.org

4.5 输出结果

结果将保存为CSV文件,包含以下字段:

  • domain_name: 域名
  • ICP: ICP备案号
  • ICP_company: 备案企业名称
  • Baidu_weight: 百度权重
  • yidong_weight: 移动权重
  • 360_weight: 360权重
  • SM_weight: 神马权重
  • sogou_weight: 搜狗权重
  • google_weight: 谷歌PR

5. 反爬策略应对

  1. 模拟真实用户行为:

    • 使用ChromeDriver完整模拟浏览器环境
    • 添加合理的等待时间(WebDriverWait)
  2. 请求失败重试机制:

    • 主查询函数(Weight_query)失败后自动调用备用查询函数(Weight_query_B)
    • 记录所有查询失败的域名
  3. 请求间隔控制:

    • 通过多线程控制并发数量(最多10个线程)
    • 避免过于频繁的请求触发反爬

6. 性能优化

  1. 多线程并发:

    • 使用ThreadPoolExecutor管理线程池
    • 通过tqdm显示进度条
  2. 数据处理优化:

    • 主域名提取(tldextract)
    • IP地址过滤(正则表达式)
    • 数据去重(set)
  3. 资源管理:

    • 及时关闭ChromeDriver实例
    • 限制最大线程数(10个)

7. 异常处理

  1. 文件读取异常:

    • 检查文件路径和格式
    • 提供友好的错误提示
  2. 网络请求异常:

    • 设置超时时间(10秒)
    • 失败后自动重试一次
  3. 数据解析异常:

    • 使用try-except捕获解析错误
    • 记录解析失败的域名

8. 扩展与改进建议

  1. 代理支持:

    • 添加代理池功能应对IP封锁
  2. 验证码识别:

    • 集成OCR或打码平台应对验证码
  3. 请求频率控制:

    • 添加随机延迟模拟人工操作
  4. 结果分析:

    • 添加数据统计和可视化功能
  5. 分布式扩展:

    • 支持分布式部署提高爬取规模

9. 注意事项

  1. 遵守爱站的使用条款,避免滥用
  2. 控制请求频率,避免对目标网站造成过大压力
  3. 确保ChromeDriver版本与本地Chrome浏览器匹配
  4. 商业用途需获得爱站官方授权

10. 完整代码获取

项目完整代码可通过原链接获取,开发者还提供了打包好的exe版本方便使用。

爱站信息爬取工具开发教学文档 1. 项目概述 本项目是一个基于ChromeDriver的爱站(aizhan.com)信息爬取工具,主要用于批量查询域名的权重信息。该工具能够有效应对爱站的反爬机制,通过多线程技术提高爬取效率,适用于网络安全领域的漏洞挖掘和资产信息收集工作。 2. 核心功能 批量查询域名在爱站的权重信息 获取ICP备案信息和企业名称 支持百度权重、移动权重、360权重、神马权重、搜狗权重和谷歌PR查询 多线程并发处理提高效率 自动处理主域名提取和去重 友好的进度显示和结果输出 3. 技术实现 3.1 主要技术栈 Python 3.x Selenium WebDriver ChromeDriver 多线程(concurrent.futures) XPath数据提取 正则表达式处理 3.2 核心模块设计 3.2.1 文件读取模块(file_ Read) 3.2.2 数据处理模块(data_ Processing) 3.2.3 多线程处理模块(Multi_ threading) 3.2.4 权重查询模块(Weight_ query和Weight_ query_ B) 3.2.5 数据写入模块(Data_ writing) 3.3 ChromeDriver配置优化 4. 使用说明 4.1 安装依赖 4.2 下载ChromeDriver 根据本地Chrome浏览器版本下载对应的ChromeDriver,并放在项目目录下。 4.3 命令行参数 单域名查询: 批量查询(从文件): 4.4 输入文件格式 domains.txt示例: 4.5 输出结果 结果将保存为CSV文件,包含以下字段: domain_ name: 域名 ICP: ICP备案号 ICP_ company: 备案企业名称 Baidu_ weight: 百度权重 yidong_ weight: 移动权重 360_ weight: 360权重 SM_ weight: 神马权重 sogou_ weight: 搜狗权重 google_ weight: 谷歌PR 5. 反爬策略应对 模拟真实用户行为 : 使用ChromeDriver完整模拟浏览器环境 添加合理的等待时间(WebDriverWait) 请求失败重试机制 : 主查询函数(Weight_ query)失败后自动调用备用查询函数(Weight_ query_ B) 记录所有查询失败的域名 请求间隔控制 : 通过多线程控制并发数量(最多10个线程) 避免过于频繁的请求触发反爬 6. 性能优化 多线程并发 : 使用ThreadPoolExecutor管理线程池 通过tqdm显示进度条 数据处理优化 : 主域名提取(tldextract) IP地址过滤(正则表达式) 数据去重(set) 资源管理 : 及时关闭ChromeDriver实例 限制最大线程数(10个) 7. 异常处理 文件读取异常 : 检查文件路径和格式 提供友好的错误提示 网络请求异常 : 设置超时时间(10秒) 失败后自动重试一次 数据解析异常 : 使用try-except捕获解析错误 记录解析失败的域名 8. 扩展与改进建议 代理支持 : 添加代理池功能应对IP封锁 验证码识别 : 集成OCR或打码平台应对验证码 请求频率控制 : 添加随机延迟模拟人工操作 结果分析 : 添加数据统计和可视化功能 分布式扩展 : 支持分布式部署提高爬取规模 9. 注意事项 遵守爱站的使用条款,避免滥用 控制请求频率,避免对目标网站造成过大压力 确保ChromeDriver版本与本地Chrome浏览器匹配 商业用途需获得爱站官方授权 10. 完整代码获取 项目完整代码可通过原链接获取,开发者还提供了打包好的exe版本方便使用。