当前位置: 首页 > news >正文

爬虫学习日记第八篇(爬取fofa某端口的协议排行及其机器数目,统计top200协议)

需求

找到最常用的200个协议
在这里插入图片描述
通过fofa搜索端口,得到协议排名前五名和对应机器的数目。
遍历端口,统计各个协议对应的机器数目(不准,但能看出个大概)

读写API

API需要会员,一天只能访问1000次。

import base64
import urllib
from time import sleep
import requests
res = {}
def onePort(j):text = 'port="' + str(j) + '"'text = base64.b64encode(text.encode("utf-8")).decode("utf-8")text = urllib.parse.quote(text)URL = f'https://fofa.info/api/v1/search/stats?fields=protocol&qbase64={text}&email=*****&key=*****'r = requests.get(URL)response_dict = r.json()print("当前端口为:",j)print(response_dict)protocols=response_dict['aggs']['protocol']for i in protocols:if i['name'] in res:res[i['name']] = res[i['name']] + i['count']else:res[i['name']] = i['count']print(res)for i in range(1,65535):onePort(i)sleep(10)

爬虫

页面动态加载,由于动态渲染的问题,有的请求返回结果为空。

单线程,未登录爬虫代码


import base64
import json
import urllib
from concurrent.futures import ThreadPoolExecutorfrom selenium import webdriver
from selenium.webdriver.chrome.service import Service
from lxml import etree
from time import sleep
#直接添加这四行代码
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
# options.add_argument('--disable-gpu')failed=[]
success=[]
res = {}
def onePort(j):s = Service(r".\chromedriver.exe")driver = webdriver.Chrome(service=s,options=options)text = 'port="' + str(j) + '"'text = base64.b64encode(text.encode("utf-8")).decode("utf-8")text = urllib.parse.quote(text)print(text)driver.get("https://fofa.info/result?qbase64=" + text)sleep(7)page_text = driver.page_source# print(page_text)tree = etree.HTML(page_text)protos = tree.xpath('//div[@class="hsxa-ui-component hsxa-meta-data-statistical-list hsxa-pos-rel"]/div[13]//li//a/text()')nums = tree.xpath('//div[@class="hsxa-ui-component hsxa-meta-data-statistical-list hsxa-pos-rel"]/div[13]//li//span/text()')for i in range(len(protos)):protos[i] = protos[i].strip(' ')protos[i] = protos[i].strip('\n')protos[i] = protos[i].strip(' ')nums[i] = nums[i].strip(' ')nums[i] = nums[i].strip('\n')nums[i] = nums[i].strip(' ')nums[i] = nums[i].replace(',', '')nums[i] = int(nums[i])if protos[i] in res:res[protos[i]] = res[protos[i]] + nums[i]else:res[protos[i]] = nums[i]print(protos)print(nums)if len(protos) == 0:failed.append(j)else:success.append(j)print("当前端口号:", j)print("失败列表:", failed)print("成功列表:", success)print(res)driver.quit()for j in range(5000,10000):onePort(j)

多线程未登录代码

一定要注意多线程同时读写问题,全局变量上锁

import base64
import json
import urllib
from concurrent.futures import ThreadPoolExecutor
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from lxml import etree
from time import sleep
import threading# 直接添加这四行代码
from selenium.webdriver.chrome.options import Optionsoptions = Options()
options.add_argument('--headless')
# options.add_argument('--disable-gpu')failed = []
success = []
res = {}
lock = threading.Lock()  # 创建线程锁def onePort(j):s = Service(r".\chromedriver.exe")driver = webdriver.Chrome(service=s, options=options)text = 'port="' + str(j) + '"'text = base64.b64encode(text.encode("utf-8")).decode("utf-8")text = urllib.parse.quote(text)print(text)driver.get("https://fofa.info/result?qbase64=" + text)sleep(7)page_text = driver.page_source# print(page_text)tree = etree.HTML(page_text)protos = tree.xpath('//div[@class="hsxa-ui-component hsxa-meta-data-statistical-list hsxa-pos-rel"]/div[13]//li//a/text()')nums = tree.xpath('//div[@class="hsxa-ui-component hsxa-meta-data-statistical-list hsxa-pos-rel"]/div[13]//li//span/text()')with lock:  # 使用线程锁保护对res变量的读写操作for i in range(len(protos)):protos[i] = protos[i].strip(' ')protos[i] = protos[i].strip('\n')protos[i] = protos[i].strip(' ')nums[i] = nums[i].strip(' ')nums[i] = nums[i].strip('\n')nums[i] = nums[i].strip(' ')nums[i] = nums[i].replace(',', '')nums[i] = int(nums[i])if protos[i] in res:res[protos[i]] = res[protos[i]] + nums[i]else:res[protos[i]] = nums[i]print(protos)print(nums)if len(protos) == 0:failed.append(j)else:success.append(j)print("当前端口号:", j)print("失败列表:", failed)print("成功列表:", success)print(res)driver.quit()with ThreadPoolExecutor(30) as t:for j in range(10000,10500):# 把下载任务提交给线程池t.submit(onePort, j)

手动登录获取cookie代码

# 填写webdriver的保存目录
s = Service(r".\chromedriver.exe")
driver= webdriver.Chrome(service=s)
# 记得写完整的url 包括http和https
driver.get('https://fofa.info')
# 程序打开网页后20秒内 “手动登陆账户”
time.sleep(20)
with open('cookies.txt','w') as f:# 将cookies保存为json格式f.write(json.dumps(driver.get_cookies()))driver.close()

登录账号的单线程爬虫


from selenium import webdriver
import time
import jsonfrom selenium.webdriver.chrome.service import Serviceimport base64
import json
import urllib
from concurrent.futures import ThreadPoolExecutor
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from lxml import etree
from time import sleep
from selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.options import Options
options = Options()
# options.add_argument('--headless')
# options.add_argument('--disable-gpu')
options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46"')failed=[]
success=[]
res = {}s = Service(r".\chromedriver.exe")
driver = webdriver.Chrome(service=s, options=options)driver.get('https://fofa.info')
# 首先清除由于浏览器打开已有的cookies
driver.delete_all_cookies()with open('cookies.txt', 'r') as f:# 使用json读取cookies 注意读取的是文件 所以用load而不是loadscookies_list = json.load(f)# 将expiry类型变为intfor cookie in cookies_list:# 并不是所有cookie都含有expiry 所以要用dict的get方法来获取if isinstance(cookie.get('expiry'), float):cookie['expiry'] = int(cookie['expiry'])driver.add_cookie(cookie)# 重新发送请求(这步是非常必要的,要不然携带完cookie之后仍然在登录界面)
driver.get('https://fofa.info')
# sleep等待页面完全加载出来,这一步很关键
time.sleep(3)j=2
text = 'port="' + str(j) + '"'
text = base64.b64encode(text.encode("utf-8")).decode("utf-8")
text = urllib.parse.quote(text)
print(text)
sleep(10)
driver.get("https://fofa.info/result?qbase64=" + text)
sleep(6)
page_text = driver.page_sourceprint(page_text)tree = etree.HTML(page_text)protos = tree.xpath('//div[@class="hsxa-ui-component hsxa-meta-data-statistical-list hsxa-pos-rel"]/div[13]//li//a/text()')
nums = tree.xpath('//div[@class="hsxa-ui-component hsxa-meta-data-statistical-list hsxa-pos-rel"]/div[13]//li//span/text()')for i in range(len(protos)):protos[i] = protos[i].strip(' ')protos[i] = protos[i].strip('\n')protos[i] = protos[i].strip(' ')nums[i] = nums[i].strip(' ')nums[i] = nums[i].strip('\n')nums[i] = nums[i].strip(' ')nums[i] = nums[i].replace(',', '')nums[i] = int(nums[i])if protos[i] in res:res[protos[i]] = res[protos[i]] + nums[i]else:res[protos[i]] = nums[i]
print(protos)
print(nums)
if len(protos) == 0:failed.append(j)
else:success.append(j)
print("当前端口号:", j)
print("失败列表:", failed)
print("成功列表:", success)
print(res)driver.quit()

登录用户多线程

import threadingfrom selenium import webdriver
import time
import jsonfrom selenium.webdriver.chrome.service import Serviceimport base64
import json
import urllib
from concurrent.futures import ThreadPoolExecutor
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from lxml import etree
from time import sleep
from selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
# options.add_argument('--disable-gpu')
options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46"')failed=[]
success=[]
res = {}
lock = threading.Lock()def onePort(j):s = Service(r".\chromedriver.exe")driver = webdriver.Chrome(service=s, options=options)driver.get('https://fofa.info')# 首先清除由于浏览器打开已有的cookiesdriver.delete_all_cookies()with open('cookies.txt', 'r') as f:# 使用json读取cookies 注意读取的是文件 所以用load而不是loadscookies_list = json.load(f)# 将expiry类型变为intfor cookie in cookies_list:# 并不是所有cookie都含有expiry 所以要用dict的get方法来获取if isinstance(cookie.get('expiry'), float):cookie['expiry'] = int(cookie['expiry'])driver.add_cookie(cookie)# 重新发送请求(这步是非常必要的,要不然携带完cookie之后仍然在登录界面)driver.get('https://fofa.info')# sleep等待页面完全加载出来,这一步很关键time.sleep(3)text = 'port="' + str(j) + '"'text = base64.b64encode(text.encode("utf-8")).decode("utf-8")text = urllib.parse.quote(text)print(text)driver.get("https://fofa.info/result?qbase64=" + text)sleep(6)page_text = driver.page_sourcetree = etree.HTML(page_text)protos = tree.xpath('//div[@class="hsxa-ui-component hsxa-meta-data-statistical-list hsxa-pos-rel"]/div[13]//li//a/text()')nums = tree.xpath('//div[@class="hsxa-ui-component hsxa-meta-data-statistical-list hsxa-pos-rel"]/div[13]//li//span/text()')with lock:  # 使用线程锁保护对res变量的读写操作for i in range(len(protos)):protos[i] = protos[i].strip(' ')protos[i] = protos[i].strip('\n')protos[i] = protos[i].strip(' ')nums[i] = nums[i].strip(' ')nums[i] = nums[i].strip('\n')nums[i] = nums[i].strip(' ')nums[i] = nums[i].replace(',', '')nums[i] = int(nums[i])if protos[i] in res:res[protos[i]] = res[protos[i]] + nums[i]else:res[protos[i]] = nums[i]print(protos)print(nums)if len(protos) == 0:failed.append(j)else:success.append(j)print("当前端口号:", j)print("失败列表:", failed)print("成功列表:", success)print(res)driver.quit()with ThreadPoolExecutor(1) as t:for j in range(3679,4000):# 把下载任务提交给线程池t.submit(onePort, j)

相关文章:

爬虫学习日记第八篇(爬取fofa某端口的协议排行及其机器数目,统计top200协议)

需求 找到最常用的200个协议 通过fofa搜索端口,得到协议排名前五名和对应机器的数目。 遍历端口,统计各个协议对应的机器数目(不准,但能看出个大概) 读写API API需要会员,一天只能访问1000次。 import…...

LeetCode 1425. 带限制的子序列和【动态规划,单调队列优化】2032

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章…...

强化学习问题(7)--- Python和Pytorch,Tensorflow的版本对应

1.问题 之前下载的python3.8,在对应Pytorch和Tensorflow时没太在意版本,在运行一些代码时,提示Pytorch和Tensorflow版本过高,直接降下来,有时候又和Python3.8不兼容,所以又在虚拟环境搞一个Pyhon3.7&#x…...

Python —— UI自动化之使用JavaScript进行元素点亮、修改、点击元素

1、JavaScript点亮元素 在控制台通过JavaScript语言中对元素点亮效果如下: 将这个语句和UI自动化结合,代码如下: locator (By.ID,"kw") # 是元组类型 web_element WebDriverWait(driver,5,0.5).until(EC.visibility_of_eleme…...

input表单的23个type属性

在HTML中&#xff0c;<input>标签用于创建输入框。type属性用于指定输入框的类型。以下是23个可能的type属性及其用途&#xff1a; text&#xff1a;普通文本输入框。password&#xff1a;密码输入框&#xff0c;输入的内容会显示为圆点或星号。email&#xff1a;电子邮…...

优先级总结

目录 越小越优先 1.路由协议 2.路由开销 3.STP 4.Ethernet-trunk&#xff08;LACP&#xff09; 越大越优先 1.VRRP 2.Router-id 3.DR/BDR 越小越优先 1.路由协议 取值范围&#xff1a;0~255 直连路由0 静态路由/默认路由60 RIP路由100 OSPF路由10或150 BGP路由255 2…...

Windows 11中无法通过默认应用更改文件关联

这里写自定义目录标题 现象解决方法 这里以.md格式文件为例。 现象 在 Windows 11 计算机上安装第三方软件后&#xff0c;关联 JPG、JPE、JPEG、PNG、MPG、MPEG、MD 等文件类型和其他文件类型的能力可能会受到阻碍。以下是尝试更改上述文件类型的文件关联时可能遇到的问题。 …...

小插曲 -- 使用Visual Studio Code远程连接香橙派

在之前的学习中&#xff0c;代码的修改和保存都依赖于“vi”指令&#xff0c;而不得不承认vi指令的编辑界面非常原始&#xff0c;所以&#xff0c;如果可以将代码编辑放到更友好的环境里进行无疑是一件大快人心的事情。 本节介绍如何通过Visual Studio Code来进行远程连接: Vi…...

留意差距:弥合网络安全基础设施的挑战

您最近一直在关注日益增加的网络威胁吗&#xff1f;如果您发现自己沉浸在 IT 或技术中&#xff0c;那么您可能会永远追求与时俱进。每天都会出现新的漏洞&#xff0c;这对保持消息灵通提出了巨大的挑战。 构建和维护能够应对复杂攻击者的网络安全基础设施所面临的挑战是真实存…...

【vSphere 8 自签名证书】企业 CA 签名证书替换 vSphere Machine SSL 证书Ⅰ—— 生成 CSR

目录 替换拓扑图证书关系示意图说明 & 关联博文 1. 默认证书截图2. 使用certificate-manager生成CSR2.1 创建存放CSR的目录2.2 记录PNID和IP2.3 生成CSR2.4 验证CSR 参考资料 替换拓扑图 证书关系示意图 默认情况下&#xff0c;VMCA 与 Machine SSL的关系是 本系列博文要…...

TypeScript中extends的用法

介绍 extends 关键字在 TypeScript 中有多种应用&#xff0c;包括泛型约束、继承类、接口继承和条件类型。通过灵活使用 extends&#xff0c;TypeScript 提供了丰富的工具来增强类型安全性&#xff0c;使代码更具表现力和可维护性。 1. 约束接口的继承 extends 关键字也可用于…...

手把手创建属于自己的ASP.NET Croe Web API项目

第一步&#xff1a;创建项目的时候选择ASP.NET Croe Web API 点击下一步&#xff0c;然后配置&#xff1a; 下一步&#xff1a;...

【Javascript】数组的基本操作

目录 声明 字面量形式 构造函数声明 访问数组中的元素 数组的长度 增删改查 增 通过索引添加数据 在数组后面添加数据 在数组前添加数据 删 删除数组中最后一个元素 删除数组中第一个元素 改 查 数组是⼀种列表对象&#xff0c;它的原型中提供了遍历和修改元素的…...

Jupyter Notebook 设置黑色背景主题

Jupyter Notebook 设置黑色背景主题 # 包安装 pip install jupyterthemes -i https://mirrors.aliyun.com/pypi/simple pip install --upgrade jupyterthemes # 查看可用主题 jt -l # monokai暗背景&#xff0c;-f(字体) -fs(字体大小) -cellw(占屏比或宽度) -ofs(输出段的字…...

1 Go的前世今生

概述 Go语言正式发布于2009年11月&#xff0c;由Google主导开发。它是一种针对多处理器系统应用程序的编程语言&#xff0c;被设计成一种系统级语言&#xff0c;具有非常强大和有用的特性。Go语言的程序速度可以与C、C相媲美&#xff0c;同时更加安全&#xff0c;支持并行进程。…...

面试-Redis-缓存击穿

问&#xff1a;什么是缓存击穿 ? 怎么解决 ? 答&#xff1a;缓存击穿的意思是对于设置时间过期的key&#xff0c;当key过期时&#xff0c;恰好有大量对这个key的请求发送过来&#xff0c;此时这些请求发现这个key过期&#xff0c;就会打到数据库加载数据并设置缓存&#xff…...

80个国内可用的Chatgpt网页版(2023.10.21更新)

ChatGPT&#xff1a;革命性的人工智能语言模型 ChatGPT&#xff0c;一款能够与人类进行自然流畅对话的人工智能语言模型&#xff0c;通过大量训练数据和先进算法&#xff0c;展现出卓越的自然语言处理能力。它能理解并回应人类问题&#xff0c;提供准确、连贯且有意义的答案&a…...

Android 10.0 Launcher3定制化之动态时钟图标功能实现

1.概述 在10.0的系统产品rom定制化开发中,在Launcher3中的定制化的一些功能中,对于一些产品要求需要实现动态时钟图标功能,这就需要先绘制时分秒时针表盘,然后 每秒刷新一次时钟图标,时钟需要做到实时更新,做到动态时钟的效果,接下来就来分析这个功能的实现 如图: 2.动…...

HTTPS、SSL/TLS,HTTPS运行过程,RSA加密算法,AES加密算法

1、为什么网站要使用安全证书 我们所处的网络环境是复杂多样的&#xff0c;大致分为两类&#xff0c;一类是可信的网络服务商&#xff0c;比如直接连的电信运营商的网络&#xff0c;网线&#xff0c;4G&#xff0c;5G&#xff1b;另一类是不可信的网络&#xff0c;比如WIFI&am…...

python之Scrapy爬虫案例:豆瓣

运行命令创建项目&#xff1a;scrapy startproject scrapySpider进入项目目录&#xff1a;cd .\scrapySpider\运行命令创建爬虫&#xff1a;scrapy genspider douban movie.douban.com目录结构说明|-- scrapySpider 项目目录 | |-- scrapySpider 项目目录 | | |-- spider…...

RocketMQ延迟消息机制

两种延迟消息 RocketMQ中提供了两种延迟消息机制 指定固定的延迟级别 通过在Message中设定一个MessageDelayLevel参数&#xff0c;对应18个预设的延迟级别指定时间点的延迟级别 通过在Message中设定一个DeliverTimeMS指定一个Long类型表示的具体时间点。到了时间点后&#xf…...

CMake基础:构建流程详解

目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...

服务器硬防的应用场景都有哪些?

服务器硬防是指一种通过硬件设备层面的安全措施来防御服务器系统受到网络攻击的方式&#xff0c;避免服务器受到各种恶意攻击和网络威胁&#xff0c;那么&#xff0c;服务器硬防通常都会应用在哪些场景当中呢&#xff1f; 硬防服务器中一般会配备入侵检测系统和预防系统&#x…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例

文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...

Nginx server_name 配置说明

Nginx 是一个高性能的反向代理和负载均衡服务器&#xff0c;其核心配置之一是 server 块中的 server_name 指令。server_name 决定了 Nginx 如何根据客户端请求的 Host 头匹配对应的虚拟主机&#xff08;Virtual Host&#xff09;。 1. 简介 Nginx 使用 server_name 指令来确定…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明

AI 领域的快速发展正在催生一个新时代&#xff0c;智能代理&#xff08;agents&#xff09;不再是孤立的个体&#xff0c;而是能够像一个数字团队一样协作。然而&#xff0c;当前 AI 生态系统的碎片化阻碍了这一愿景的实现&#xff0c;导致了“AI 巴别塔问题”——不同代理之间…...

Matlab | matlab常用命令总结

常用命令 一、 基础操作与环境二、 矩阵与数组操作(核心)三、 绘图与可视化四、 编程与控制流五、 符号计算 (Symbolic Math Toolbox)六、 文件与数据 I/O七、 常用函数类别重要提示这是一份 MATLAB 常用命令和功能的总结,涵盖了基础操作、矩阵运算、绘图、编程和文件处理等…...

聊一聊接口测试的意义有哪些?

目录 一、隔离性 & 早期测试 二、保障系统集成质量 三、验证业务逻辑的核心层 四、提升测试效率与覆盖度 五、系统稳定性的守护者 六、驱动团队协作与契约管理 七、性能与扩展性的前置评估 八、持续交付的核心支撑 接口测试的意义可以从四个维度展开&#xff0c;首…...

Redis数据倾斜问题解决

Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中&#xff0c;部分节点存储的数据量或访问量远高于其他节点&#xff0c;导致这些节点负载过高&#xff0c;影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...

3-11单元格区域边界定位(End属性)学习笔记

返回一个Range 对象&#xff0c;只读。该对象代表包含源区域的区域上端下端左端右端的最后一个单元格。等同于按键 End 向上键(End(xlUp))、End向下键(End(xlDown))、End向左键(End(xlToLeft)End向右键(End(xlToRight)) 注意&#xff1a;它移动的位置必须是相连的有内容的单元格…...