python脚本 检测空页面

检测空页面 只需要检测页面是否有 必有的元素即可。

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import requests
import time
import socket


def verificateOmWeb():
    url = "此处需要url替换"
    headers = {
        "Cookie": "此处需要Cookie替换;"
    }
    req = requests.get(url=url, headers=headers)
    # 获取到网页的内容,是string类型
    result = req.content

    # 解析一下这个域名对应的ip地址
    myaddr = socket.getaddrinfo("此处需要域名替换", 'http')
    webIp = myaddr[0][4][0]

    # 校验网页的string内容中有自己想要的值,为了防止页面为空
    if ("此处需要 必填元素或必填文本 替换" in result):
        print("web is success,页面不为空")
    else:
        print("web content is none,页面为空")
    return webIp


def readFile():
    omiphosts = [
        "ip替换1 域名替换",
        "ip替换2 域名替换",
        "ip替换3 域名替换"
    ]

    for omiphost in omiphosts:
        file_data = ""
        ip_data = ""

        # 读的形式打开本地的Hosts文件,读到需要修改的host行,进行修改替换
        with open("C:\Windows\System32\drivers\etc\hosts", "r") as f:
            for line in f:
                if "域名替换" in line:
                    line = line.replace(line, omiphost)
                file_data += line
        # 写的形式打开文件,将替换的语句写进去
        with open("C:\Windows\System32\drivers\etc\hosts", "w") as f:
            f.write(file_data)

        # 检查写的Hosts是否写进去了
        with open("C:\Windows\System32\drivers\etc\hosts", "r") as f:
            fread = f.read()
            if omiphost in fread:
                print(omiphost + "  :hosts switch sucess,成功写入Hosts")
            else:
                print(omiphost + " :hosts is not switch sucess,,写入Hosts失败")

        time.sleep(10)
        ip_data = verificateOmWeb()

        # 检查获取网页的ip地址与hosts切换的ip地址是一样的。
        if ip_data in omiphost:
            print(ip_data + "  web hosts sucess ip一样")
        else:
            print("web hosts is not equal your hosts ip不一样")


if __name__ == '__main__':
    readFile()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值