Python 检查 TXT 文件中死链接的完整代码
以下是一个 Python 程序,可以检查指定的 txt 文件中的链接是否为死链:
import requests
filename = 'links.txt' # 要检查的文件名
def check_links(filename):
with open(filename, 'r') as f:
links = f.readlines()
for link in links:
link = link.strip() # 去掉换行符
try:
response = requests.get(link)
if response.status_code == 200:
print(link + ' is a valid link.')
else:
print(link + ' is a dead link.')
except requests.exceptions.RequestException as e:
print(link + ' is a dead link.')
check_links(filename)
该程序使用 requests 库发送 HTTP 请求来检查链接的有效性。如果链接返回状态码为 200,则认为它是有效的链接。如果链接无法访问或返回状态码为非 200,则认为它是死链接。
原文地址: https://www.cveoy.top/t/topic/oYxq 著作权归作者所有。请勿转载和采集!