#!/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)
python检查URL是否能正常访问
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 做SEM的如果账户比较大着陆页会比较多。做医疗行业的那些账户通常是专门配置了竞价网站,就是说把优化站和竞价站分开来...