当前位置: 首页 > 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…...

【位运算】消失的两个数字(hard)

消失的两个数字&#xff08;hard&#xff09; 题⽬描述&#xff1a;解法&#xff08;位运算&#xff09;&#xff1a;Java 算法代码&#xff1a;更简便代码 题⽬链接&#xff1a;⾯试题 17.19. 消失的两个数字 题⽬描述&#xff1a; 给定⼀个数组&#xff0c;包含从 1 到 N 所有…...

无法与IP建立连接,未能下载VSCode服务器

如题&#xff0c;在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈&#xff0c;发现是VSCode版本自动更新惹的祸&#xff01;&#xff01;&#xff01; 在VSCode的帮助->关于这里发现前几天VSCode自动更新了&#xff0c;我的版本号变成了1.100.3 才导致了远程连接出…...

Linux云原生安全:零信任架构与机密计算

Linux云原生安全&#xff1a;零信任架构与机密计算 构建坚不可摧的云原生防御体系 引言&#xff1a;云原生安全的范式革命 随着云原生技术的普及&#xff0c;安全边界正在从传统的网络边界向工作负载内部转移。Gartner预测&#xff0c;到2025年&#xff0c;零信任架构将成为超…...

【HTTP三个基础问题】

面试官您好&#xff01;HTTP是超文本传输协议&#xff0c;是互联网上客户端和服务器之间传输超文本数据&#xff08;比如文字、图片、音频、视频等&#xff09;的核心协议&#xff0c;当前互联网应用最广泛的版本是HTTP1.1&#xff0c;它基于经典的C/S模型&#xff0c;也就是客…...

Android 之 kotlin 语言学习笔记三(Kotlin-Java 互操作)

参考官方文档&#xff1a;https://developer.android.google.cn/kotlin/interop?hlzh-cn 一、Java&#xff08;供 Kotlin 使用&#xff09; 1、不得使用硬关键字 不要使用 Kotlin 的任何硬关键字作为方法的名称 或字段。允许使用 Kotlin 的软关键字、修饰符关键字和特殊标识…...

Web 架构之 CDN 加速原理与落地实践

文章目录 一、思维导图二、正文内容&#xff08;一&#xff09;CDN 基础概念1. 定义2. 组成部分 &#xff08;二&#xff09;CDN 加速原理1. 请求路由2. 内容缓存3. 内容更新 &#xff08;三&#xff09;CDN 落地实践1. 选择 CDN 服务商2. 配置 CDN3. 集成到 Web 架构 &#xf…...

Python ROS2【机器人中间件框架】 简介

销量过万TEEIS德国护膝夏天用薄款 优惠券冠生园 百花蜂蜜428g 挤压瓶纯蜂蜜巨奇严选 鞋子除臭剂360ml 多芬身体磨砂膏280g健70%-75%酒精消毒棉片湿巾1418cm 80片/袋3袋大包清洁食品用消毒 优惠券AIMORNY52朵红玫瑰永生香皂花同城配送非鲜花七夕情人节生日礼物送女友 热卖妙洁棉…...

Python基于历史模拟方法实现投资组合风险管理的VaR与ES模型项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档&#xff09;&#xff0c;如需数据代码文档可以直接到文章最后关注获取。 1.项目背景 在金融市场日益复杂和波动加剧的背景下&#xff0c;风险管理成为金融机构和个人投资者关注的核心议题之一。VaR&…...

Mysql8 忘记密码重置,以及问题解决

1.使用免密登录 找到配置MySQL文件&#xff0c;我的文件路径是/etc/mysql/my.cnf&#xff0c;有的人的是/etc/mysql/mysql.cnf 在里最后加入 skip-grant-tables重启MySQL服务 service mysql restartShutting down MySQL… SUCCESS! Starting MySQL… SUCCESS! 重启成功 2.登…...

aardio 自动识别验证码输入

技术尝试 上周在发学习日志时有网友提议“在网页上识别验证码”&#xff0c;于是尝试整合图像识别与网页自动化技术&#xff0c;完成了这套模拟登录流程。核心思路是&#xff1a;截图验证码→OCR识别→自动填充表单→提交并验证结果。 代码在这里 import soImage; import we…...