链接可能由于种种原因失效,本文记录 Python 验证链接有效性的方法。
request
from urllib import request
try:
with request.urlopen(
"https://www.baidu.com/") as file:
print(file.status)
print(file.reason)
except Exception as e:
print(e)
返回结果:
- 有效链接:
200
OK
- 失效链接:
HTTP Error 404: Not Found
urllib URL can’t contain control characters.
- 错误:
urllib URL can't contain control characters.
request.urlopen(your_url)
其中 your_url
中不能包含空格等字符
- 解决方案:用
%20
替换空格
参考资料
参考资料
- https://blog.csdn.net/haiyi_guo/article/details/98619536
- https://blog.csdn.net/KaliWalker/article/details/104216705