Python3 批量检查URL状态(status code) 是否为200 OK

批量检测 URL 状态,并展示哪些 URL 状态为非 200 OK

# -*- coding: utf-8 -*-

import requests
import urllib3


def get_code_status(urls_file):
    """
    检测urls状态码
    :param urls_file: urls文件
    :return: 状态码
    allow_redirects: 拒绝默认的301/302重定向从而可以通过 html.headers[‘Location’] 拿到重定向的 URL。
    verify: 取消证书认证
    """
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3298.4 Safari/537.36'
    }
    with open(urls_file, 'r', encoding='utf-8') as f:
        urls_data = f.readlines()
        for url in urls_data:
            url = url.strip('\n')
            disable_warnings()
            try:
                res = requests.get(
                    url, headers=header, verify=False, allow_redirects=False
                )
            except Exception as error:
                print(error)
            code = res.status_code
            if code != 200:
                print('状态码 {}:{}'.format(code, url))
    return code


def disable_warnings():
    """
    解除去掉证书后总是抛出异常告警
    """
    urllib3.disable_warnings()


if __name__ == '__main__':
    get_code_status('./urls.txt')

你可能感兴趣的:(Python)