【记录】使用 Python 爬取 Malpedia 信息

说明

由于业务需要获取近三年勒索家族病毒信息,因此尝试使用 python 爬取 Malpedia 的家族列表

代码首先检查是否为 Windows 或 Linux 系统,然后获取详细信息页面路径并访问,检查页面中是否包含 ransom 字符串,并检查最新报告时间是否在 2021 年之后,如果条件全部满足,则记录家族名称及详细信息页面路径。

注意,由于检查某家族是否和 勒索 相关的方式只是通过 “页面中是否包含 ransom 字符串” 的方式,因此结果并不准确,可能会有漏报或误报,误报的方式通过人工再次检查,漏报不做处理

代码

from bs4 import BeautifulSoup
import urllib.request
import warnings

warnings.filterwarnings("ignore")
base_url = 'https://malpedia.caad.fkie.fraunhofer.de'
url = base_url + '/families'

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'}


def get_page(url):
    # 获取详细信息页面
    req = urllib.request.Request(url=url, headers=headers)
    res = urllib.request.urlopen(req, timeout=100)
    html=res.read().decode('utf-8')
    soup=BeautifulSoup(html)

    # 检查页面是否包含 ransom 字符串
    content = soup.select('.col-xl-12')
    content = [str(x) for x in content]
    content = ''.join(content)
    #print(content)
    if 'ransom' not in content.lower():
        return False

    # 检查时间是否在 2021 年之后
    for item in soup.select('.clickable-row-newtab'):
        date = item.select('.date')[0].text
        year = int(date.split('-')[0])
        if year > 2020:
            return True
    return False

if __name__ == "__main__":
    f = open('result.txt', 'w')

    # 获取主页面
    req = urllib.request.Request(url=url, headers=headers)
    res = urllib.request.urlopen(req, timeout=100)
    html=res.read().decode('utf-8')
    soup=BeautifulSoup(html)
    # 检查每个病毒家族信息
    for item in soup.select('.clickable-row'):
        os = item.select('.os > .fa-windows')  # 检查操作系统信息
        if not os:
            os = item.select('.os > .fa-linux')
            if not os:
                continue
        title = item.select('.common_name')[0].text  # 获得家族名称
        title = title.strip()
        page = base_url + item['data-href']  # 获得详细信息页面路径
        print(title)
        #print(page)

        status = get_page(page)

        if status:
            f.write(title + '\t' + page + '\n')  # 将结果写入文件
            print(title + '\t' + page + '\n')
        #time.sleep(1)

    f.close()
  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zephyrOOO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值