#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File :test_-pytest-automation -> 555
@IDE :PyCharm
@Author :Mr. MA
@Date :2021/7/30 9:37
@Desc :检查URL是否能正常访问(脚本)
=================================================='''
import requests
import pandas as pd
# 取出excel表格数据遍历后生成字典
def parse_excel(file_path, sheet_name, column1, column2):
res = {}
data = pd.read_excel(io=file_path, sheet_name=sheet_name)
a = data[column1]
c = data[column2]
for i in range(len(a)):
res[a[i]] = (str(c[i]))
return res
# 检查URL状态
def getHttpStatusCode(url):
try:
request = requests.get(url)
httpStatusCode = request.status_code
return httpStatusCode
except requests.exceptions.HTTPError as e:
return e
if __name__ == "__main__":
real_data = parse_excel('url.xls', 'Sheet1', 'id', 'product_show_img')
# 取字典里数据遍历key,value检验url,不通链接保存至文本输出
for key, value in real_data.items():
try:
status = getHttpStatusCode(value.strip('\n')) # 换行符
if status != 200:
with open('403.txt', 'a') as f:
# f.write("id:{},product_show_img:{}".format(key,value) + '\n')
f.write("{},".format(key) + '\n')
print(key, value)
else:
print('sucess')
except Exception as e:
print(e)