不多说了,直接上代码,使用Python3。
有些时候收集的网址很多,等我们整理入库的时候可能已经失效了,在录入到网站里的时候我们会做一次有效检测,确保录入的时候是能访问的。
import openpyxl
import requests
from ping3 import ping
# 打开Excel文件
wb = openpyxl.load_workbook('your_excel_file.xlsx')
# 选择工作表
sheet = wb['sheet1']
# 指定列的网址数据范围
url_column = 'B'
# 遍历每个单元格
for cell in sheet[url_column]:
# 获取网址
url = cell.value
# 检测网址有效性
try:
response = requests.get(url)
if response.status_code == 200:
cell.offset(column=1).value = '有效'
else:
cell.offset(column=1).value = '无效'
except:
cell.offset(column=1).value = '无效'
# 执行ping命令检测
ping_result = ping(url)
if ping_result is not None:
cell.offset(column=2).value = '有效'
else:
cell.offset(column=2).value = '无效'
# 保存修改
wb.save('data.xlsx')
# 在控制台显示运行状态
print(f"检测完成: {url}")
# 关闭Excel文件
wb.close()
- All rights reserved.
- No part of this website, including text and images, may be reproduced, modified, distributed, or transmitted in any form or by any means, without the prior written permission of the author.
- Unauthorized commercial use is strictly prohibited.
- Unauthorized personal use is strictly prohibited.