本文最后更新于:2024年1月14日 晚上

链接可能由于种种原因失效,本文记录 Python 验证链接有效性的方法。

request

1
2
3
4
5
6
7
8
9
10
11

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)

返回结果:

  • 有效链接:
1
2
200
OK
  • 失效链接:
1
HTTP Error 404: Not Found

urllib URL can’t contain control characters.

  • 错误: urllib URL can't contain control characters.

    1
    request.urlopen(your_url)

    其中 your_url 中不能包含空格等字符

  • 解决方案:用 %20 替换空格

参考资料



文章链接:
https://www.zywvvd.com/notes/coding/python/valid-url/valid-url/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Python 验证链接有效性
https://www.zywvvd.com/notes/coding/python/valid-url/valid-url/
作者
Yiwei Zhang
发布于
2021年9月6日
许可协议