- A+
所属分类:python爬虫
手里的几百个网站总是出现打不开、被入侵、百度危险提醒,每次都要花大量的时间去查看,去维护,后面觉得这个方式太low,就有了下面的小爬虫。
图片来源互联网,侵删
爬虫检测范围
1、检测百度入侵
2、检测网站源代码被入侵
3、检测域名是否正常打开
学习基础
1、基础的Python语法
2、requests的基础知识
代码思路
1、通过读取页面的源代码,判断是否包含指定的内容
2、多网站直接用列表的形式存储数据,循环执行打开
3、页面错误调试
代码详细
- # 作者:李金龙
- # 网站:https://www.lijinlong.cc
- # 版本号:QYT_v1.2
- import requests
- # 列出所有查询的网址域名
- all_urls =[
- "www.qiyoutuo.com",
- ]
- 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"
- headers={"User-Agent":user_agent}
- for i in all_urls:
- domain = "http://" + i
- try:
- code = requests.get(domain)
- except:
- #调试异常情况
- print(i, "域名出现无法访问问题,请及时排查问题")
- #跳出当前循环
- continue
- else:
- if code.status_code == 200:
- #循环读取每一个网站的情况
- html = code.text
- if "<script>document.title=" in html:
- print("您的网站:",i,"已出现疑似入侵信息,请快速处理")
- #循环读取所有所有域名进行凭拼接
- baidu_domain = "https://www.baidu.com/s?ie=UTF-8&wd=site%3A" + i
- #指定访问all_urls中的所有域名
- html = requests.get(baidu_domain,headers=headers).text
- if "风险" in html:
- print(i,'域名百度已报:风险,请快速处理')
- print("网站查询完成")
扩展阅读
其实在上面更多的结果上面,其实我更需要看到的是如何把网站的目录给监控起来,但是苦在linux学习的不深,没办法直接去撸。
下一步计划了解下python的自动化运维,把很多的事情让程序去做,让自己有更多的时间,休息也好,健身也好。