网站入侵检测小爬虫,适用于百度搜索引擎,指定入侵标识

  • A+
所属分类:python爬虫

手里的几百个网站总是出现打不开、被入侵、百度危险提醒,每次都要花大量的时间去查看,去维护,后面觉得这个方式太low,就有了下面的小爬虫。

网站入侵检测小爬虫,适用于百度搜索引擎,指定入侵标识

图片来源互联网,侵删

爬虫检测范围

1、检测百度入侵

2、检测网站源代码被入侵

3、检测域名是否正常打开

学习基础

1、基础的Python语法

2、requests的基础知识

代码思路

1、通过读取页面的源代码,判断是否包含指定的内容

2、多网站直接用列表的形式存储数据,循环执行打开

3、页面错误调试

代码详细

  1. # 作者:李金龙
  2. # 网站:https://www.lijinlong.cc
  3. # 版本号:QYT_v1.2
  4. import requests
  5. # 列出所有查询的网址域名
  6. all_urls =[
  7.     "www.qiyoutuo.com",
  8. ]
  9. user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
  10. headers={"User-Agent":user_agent}
  11. for i in all_urls:
  12.     domain = "http://" + i
  13.     try:
  14.         code = requests.get(domain)
  15.     except:
  16.         #调试异常情况
  17.         print(i, "域名出现无法访问问题,请及时排查问题")
  18.         #跳出当前循环
  19.         continue
  20.     else:
  21.         if code.status_code == 200:
  22.         #循环读取每一个网站的情况
  23.             html = code.text
  24.             if "<script>document.title=" in html:
  25.                 print("您的网站:",i,"已出现疑似入侵信息,请快速处理")
  26.         #循环读取所有所有域名进行凭拼接
  27.             baidu_domain = "https://www.baidu.com/s?ie=UTF-8&wd=site%3A" + i
  28.             #指定访问all_urls中的所有域名
  29.             html = requests.get(baidu_domain,headers=headers).text
  30.             if "风险" in html:
  31.                 print(i,'域名百度已报:风险,请快速处理')
  32. print("网站查询完成")

扩展阅读

其实在上面更多的结果上面,其实我更需要看到的是如何把网站的目录给监控起来,但是苦在linux学习的不深,没办法直接去撸。

下一步计划了解下python的自动化运维,把很多的事情让程序去做,让自己有更多的时间,休息也好,健身也好。

 

李金龙

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: