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

Qwen-Turbo-BF16在QT跨平台开发中的应用:智能聊天机器人

Qwen-Turbo-BF16在QT跨平台开发中的应用&#xff1a;智能聊天机器人 1. 引言 想象一下&#xff0c;你正在开发一个需要在Windows、Linux和macOS三大平台上运行的智能聊天应用。传统的开发方式可能需要为每个平台编写不同的代码&#xff0c;维护成本高且开发周期长。而今天我们…...

思源宋体7款字重终极指南:从零基础到专业排版的完整解决方案

思源宋体7款字重终极指南&#xff1a;从零基础到专业排版的完整解决方案 【免费下载链接】source-han-serif-ttf Source Han Serif TTF 项目地址: https://gitcode.com/gh_mirrors/so/source-han-serif-ttf 还在为中文设计寻找既专业又免费的字体资源吗&#xff1f;思源…...

RHCA考试全攻略:从报名到拿证,手把手教你避开那些坑

RHCA认证深度实战指南&#xff1a;从零到架构师的系统化进阶路径 红帽认证架构师&#xff08;RHCA&#xff09;作为Linux领域的顶级认证&#xff0c;早已超越单纯的技术考核&#xff0c;成为运维工程师职业发展的分水岭。不同于市面上大多数"题库背诵式"认证&#xf…...

如何用代码驱动可视化:Mermaid Live Editor的实时图表编辑革命

如何用代码驱动可视化&#xff1a;Mermaid Live Editor的实时图表编辑革命 【免费下载链接】mermaid-live-editor Edit, preview and share mermaid charts/diagrams. New implementation of the live editor. 项目地址: https://gitcode.com/GitHub_Trending/me/mermaid-liv…...

科哥二次开发HeyGem系统:快速上手,批量生成数字人岗位视频

科哥二次开发HeyGem系统&#xff1a;快速上手&#xff0c;批量生成数字人岗位视频 1. 系统概述与核心价值 HeyGem数字人视频生成系统批量版WebUI版是经过科哥二次开发优化的AI视频合成工具&#xff0c;专为解决企业批量制作数字人视频需求而设计。这个系统最突出的特点是能够…...

TPFanCtrl2终极指南:ThinkPad风扇控制完全手册,打造静音高效的散热系统

TPFanCtrl2终极指南&#xff1a;ThinkPad风扇控制完全手册&#xff0c;打造静音高效的散热系统 【免费下载链接】TPFanCtrl2 ThinkPad Fan Control 2 (Dual Fan) for Windows 10 and 11 项目地址: https://gitcode.com/gh_mirrors/tp/TPFanCtrl2 作为ThinkPad用户&#…...

如何免费获得专业级Windows音效?Equalizer APO系统级均衡器终极指南

如何免费获得专业级Windows音效&#xff1f;Equalizer APO系统级均衡器终极指南 【免费下载链接】equalizerapo Equalizer APO mirror 项目地址: https://gitcode.com/gh_mirrors/eq/equalizerapo 你是否厌倦了每个音频应用都需要单独设置音效&#xff1f;是否希望游戏、…...

嵌入式设备部署MogFace-large轻量版:从模型压缩到板载推理

嵌入式设备部署MogFace-large轻量版&#xff1a;从模型压缩到板载推理 最近有不少朋友在问&#xff0c;能不能把那些效果不错的人脸检测模型&#xff0c;比如MogFace-large&#xff0c;塞到树莓派或者Jetson Nano这类嵌入式板子里去跑。想法很好&#xff0c;但直接把原始模型丢…...

MogFace-large多场景应用:直播美颜预处理、AI考勤系统集成案例

MogFace-large多场景应用&#xff1a;直播美颜预处理、AI考勤系统集成案例 1. 引言 你有没有遇到过这样的场景&#xff1f;在直播时&#xff0c;美颜滤镜总是识别不准你的脸&#xff0c;导致效果时好时坏&#xff1b;或者公司用的人脸考勤机&#xff0c;在光线稍暗或角度偏一…...

Day05:C语言数组存储结构与字符串详解

一、数组的存储结构1. 数组变量的地址连续性数组中的元素在内存中地址是连续的。数组名非常重要&#xff0c;涉及指针与内存操作。2. 数组名的含义数组名表示首元素的地址。示例&#xff1a;int arr[5]; printf("%p\n", arr); // 输出首元素地址 printf("%p…...