python批量获取BPB代码存档

[复制链接]
查看22 | 回复0 | 5 天前 | 显示全部楼层 |阅读模式
如果你想进行批量获取的话,那么零度给大家手搓了一个脚本,需要的自行食用
1,需要安装下Python,没有安装的可以前往【python官网】进行下载安装,推荐python3.10.3 版本
2,安装下 Python 依赖库
  1. pip install requests beautifulsoup4
  2. pip install selenium
复制代码
如果提示找不到package,那是因为你的python版本比较低,可以通过下面的安装命令解决
  1. pip install selenium requests beautifulsoup4 packaging
复制代码
3,把下面的代码另存为一个vpn.py
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from packaging import version  # 用于比较版本号

  4. # FOFA 查询页面 URL
  5. FOFA_URL = "https://fofa.info/result?qbase64=aWNvbl9oYXNoPSItMTM1NDAyNzMxOSIgJiYgYXNuPSIxMzMzNSIgJiYgcG9ydD0iNDQzIg%3D%3D"

  6. # 文件名定义
  7. OUTPUT_FILE_1 = "1.txt"
  8. OUTPUT_FILE_2 = "2.txt"
  9. OUTPUT_FILE_OK = "OK.txt"

  10. def get_fofa_results():
  11.     """抓取 FOFA 页面并提取结果地址"""
  12.     print("正在抓取 FOFA 页面...")
  13.     response = requests.get(FOFA_URL)
  14.     if response.status_code != 200:
  15.         print(f"无法访问 FOFA 页面,状态码:{response.status_code}")
  16.         return []

  17.     # 使用 BeautifulSoup 解析 HTML 内容
  18.     soup = BeautifulSoup(response.text, 'html.parser')
  19.     results = []

  20.     # 查找所有结果链接
  21.     for link in soup.find_all('a', href=True):
  22.         url = link['href']
  23.         if url.startswith("https://"):  # 提取 https 开头的链接
  24.             results.append(url)

  25.     print(f"提取到 {len(results)} 个地址")
  26.     return results

  27. def append_login_to_urls(input_file, output_file):
  28.     """将地址加上 /login 并保存到新文件"""
  29.     print(f"正在处理 {input_file},将地址加上 /login 并保存到 {output_file}...")
  30.     with open(input_file, "r", encoding="utf-8") as infile, open(output_file, "w", encoding="utf-8") as outfile:
  31.         for line in infile:
  32.             url = line.strip() + "/login"
  33.             outfile.write(url + "\n")
  34.     print(f"已完成地址追加,结果保存到 {output_file}")

  35. def check_bpb_version(input_file, output_file):
  36.     """访问地址并检查 BPB Panel 的版本号"""
  37.     print(f"正在验证 {input_file} 中的地址...")
  38.     headers = {
  39.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
  40.     }
  41.     min_version = version.parse("2.5.3")  # 最低版本号
  42.     with open(input_file, "r", encoding="utf-8") as infile, open(output_file, "w", encoding="utf-8") as outfile:
  43.         for line in infile:
  44.             url = line.strip()
  45.             try:
  46.                 response = requests.get(url, headers=headers, timeout=10)
  47.                 if response.status_code == 200 and "BPB Panel" in response.text:
  48.                     # 提取版本号
  49.                     raw_version = extract_version(response.text)
  50.                     if raw_version:
  51.                         parsed_version = version.parse(raw_version)
  52.                         if parsed_version > min_version:
  53.                             print(f"[匹配] {url} (版本号: {parsed_version})")
  54.                             outfile.write(url + "\n")
  55.             except Exception as e:
  56.                 print(f"[错误] 无法访问 {url},错误:{e}")
  57.     print(f"验证完成,符合条件的地址保存到 {output_file}")

  58. def extract_version(html):
  59.     """从 HTML 内容中提取 BPB Panel 的版本号"""
  60.     if "BPB Panel" in html:
  61.         start_index = html.find("BPB Panel")
  62.         # 提取版本号(假设版本号以 2 开头)
  63.         version_start = html.find("2", start_index)
  64.         version_end = version_start
  65.         while version_end < len(html) and (html[version_end].isdigit() or html[version_end] == "."):
  66.             version_end += 1
  67.         return html[version_start:version_end]
  68.     return None

  69. def main():
  70.     # 第一步:抓取 FOFA 页面结果并保存到 1.txt
  71.     urls = get_fofa_results()
  72.     with open(OUTPUT_FILE_1, "w", encoding="utf-8") as f:
  73.         for url in urls:
  74.             f.write(url + "\n")
  75.     print(f"FOFA 页面结果已保存到 {OUTPUT_FILE_1}")

  76.     # 第二步:将 1.txt 地址加上 /login 并保存到 2.txt
  77.     append_login_to_urls(OUTPUT_FILE_1, OUTPUT_FILE_2)

  78.     # 第三步:验证 2.txt 中的地址,符合条件的保存到 OK.txt
  79.     check_bpb_version(OUTPUT_FILE_2, OUTPUT_FILE_OK)

  80. if __name__ == "__main__":
  81.     main()
复制代码
最后在终端下执行命令:
  1. python vpn.py
复制代码
就可以在脚本的所在目录会生成结果OK.txt 里面就是我们需要的地址!
注意:默认我只获取了第一个页面,因为如果批量获取分页的话,会引起网站防火墙的拦截。会被屏蔽,所以如果你出现获取的结果是空的话,那么就是你的IP被拦截了,自行代理访问再获取!
如果你需要获取分页结果,可以使用下面的脚本:
  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. import time
  4. from packaging import version
  5. import requests
  6. from bs4 import BeautifulSoup

  7. # FOFA 查询页面基础 URL
  8. BASE_URL = "https://fofa.info/result?qbase64=aWNvbl9oYXNoPSItMTM1NDAyNzMxOSIgJiYgYXNuPSIxMzMzNSIgJiYgcG9ydD0iNDQzIg%3D%3D"

  9. # 文件名定义
  10. OUTPUT_FILE_1 = "1.txt"
  11. OUTPUT_FILE_2 = "2.txt"
  12. OUTPUT_FILE_OK = "OK.txt"

  13. def init_browser():
  14.     """初始化 Selenium 浏览器"""
  15.     options = webdriver.ChromeOptions()
  16.     options.add_argument("--headless")  # 无头模式
  17.     options.add_argument("--disable-gpu")
  18.     options.add_argument("--no-sandbox")
  19.     options.add_argument("--disable-dev-shm-usage")
  20.     driver = webdriver.Chrome(options=options)
  21.     return driver

  22. def get_fofa_results(start_page, end_page):
  23.     """使用 Selenium 抓取 FOFA 页面并提取结果地址"""
  24.     print(f"正在抓取 FOFA 页面,从第 {start_page} 页到第 {end_page} 页...")
  25.     results = []
  26.     driver = init_browser()

  27.     for page in range(start_page, end_page + 1):
  28.         print(f"正在抓取第 {page} 页...")
  29.         driver.get(f"{BASE_URL}&page={page}")
  30.         time.sleep(3)  # 等待页面加载

  31.         # 查找页面中结果的链接(通过具体的 class 筛选结果链接)
  32.         elements = driver.find_elements(By.CSS_SELECTOR, "a[href^='https://']")
  33.         for elem in elements:
  34.             url = elem.get_attribute("href")
  35.             if url not in results:
  36.                 results.append(url)

  37.     driver.quit()
  38.     print(f"总共提取到 {len(results)} 个地址")
  39.     return results

  40. def append_login_to_urls(input_file, output_file):
  41.     """将地址加上 /login 并保存到新文件"""
  42.     print(f"正在处理 {input_file},将地址加上 /login 并保存到 {output_file}...")
  43.     with open(input_file, "r", encoding="utf-8") as infile, open(output_file, "w", encoding="utf-8") as outfile:
  44.         for line in infile:
  45.             url = line.strip() + "/login"
  46.             outfile.write(url + "\n")
  47.     print(f"已完成地址追加,结果保存到 {output_file}")

  48. def check_bpb_version(input_file, output_file):
  49.     """访问地址并检查 BPB Panel 的版本号"""
  50.     print(f"正在验证 {input_file} 中的地址...")
  51.     headers = {
  52.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
  53.     }
  54.     min_version = version.parse("2.5.3")  # 最低版本号
  55.     with open(input_file, "r", encoding="utf-8") as infile, open(output_file, "w", encoding="utf-8") as outfile:
  56.         for line in infile:
  57.             url = line.strip()
  58.             try:
  59.                 response = requests.get(url, headers=headers, timeout=10)
  60.                 if response.status_code == 200 and "BPB Panel" in response.text:
  61.                     # 提取版本号
  62.                     raw_version = extract_version(response.text)
  63.                     if raw_version:
  64.                         parsed_version = version.parse(raw_version)
  65.                         if parsed_version > min_version:
  66.                             print(f"[匹配] {url} (版本号: {parsed_version})")
  67.                             outfile.write(url + "\n")
  68.             except Exception as e:
  69.                 print(f"[错误] 无法访问 {url},错误:{e}")
  70.     print(f"验证完成,符合条件的地址保存到 {output_file}")

  71. def extract_version(html):
  72.     """从 HTML 内容中提取 BPB Panel 的版本号"""
  73.     if "BPB Panel" in html:
  74.         start_index = html.find("BPB Panel")
  75.         # 提取版本号(假设版本号以 2 开头)
  76.         version_start = html.find("2", start_index)
  77.         version_end = version_start
  78.         while version_end < len(html) and (html[version_end].isdigit() or html[version_end] == "."):
  79.             version_end += 1
  80.         return html[version_start:version_end]
  81.     return None

  82. def main():
  83.     # 配置分页范围
  84.     start_page = 1  # 起始页码
  85.     end_page = 5    # 结束页码

  86.     # 第一步:抓取 FOFA 页面结果并保存到 1.txt
  87.     urls = get_fofa_results(start_page, end_page)
  88.     with open(OUTPUT_FILE_1, "w", encoding="utf-8") as f:
  89.         for url in urls:
  90.             f.write(url + "\n")
  91.     print(f"FOFA 页面结果已保存到 {OUTPUT_FILE_1}")

  92.     # 第二步:将 1.txt 地址加上 /login 并保存到 2.txt
  93.     append_login_to_urls(OUTPUT_FILE_1, OUTPUT_FILE_2)

  94.     # 第三步:验证 2.txt 中的地址,符合条件的保存到 OK.txt
  95.     check_bpb_version(OUTPUT_FILE_2, OUTPUT_FILE_OK)

  96. if __name__ == "__main__":
  97.     main()
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则