本帖最后由 苏紫方璇 于 2025-12-1 13:06 编辑
最近遇到excel文件中批量的超链接失效问题,一个一个点击会特别慢,用Python写了一个检查链接失效的程序,供大家参考,有问题请及时纠正。
[Python] 纯文本查看 复制代码 import requests
from bs4 import BeautifulSoup
import openpyxl
workbook = openpyxl.load_workbook('1.xlsx')
sheet = workbook.active
cells_1 = sheet['N1:N4464']
cells_2 = sheet['G1:G4464']
index = 0
for row in cells_1:
if row[0].hyperlink:
url = row[0].hyperlink.target
#print(url)
#print(index)
response = requests.get(url)
if response.status_code == 200:
print("success")
else:
with open('test', 'a', encoding='utf-8') as file:
#file.write(str(mm))
for aaa in cells_2[index]:
file.write(str(aaa.valu))
file.write(' ' +str(row[0]) + '\n')
else:
with open('test', 'a', encoding='utf-8') as file:
for aaa in cells_2[index]:
file.write(str(aaa.value))
file.write(' ' + str(row[0]) + '\n')
index += 1
print("end!!!!!!") |