在不使用 python 下载内容的情况下检查 url 是否存在

Check whether url exists or not without downloading the content using python

我必须使用 python 检查 url 是否存在。我正在尝试使用 requests.get(url) 但它会花费很多时间,因为一旦 get 被点击,文件就会开始下载。我不想下载文件来检查 url 有效性。这可以使用 python 来实现吗?

也许这对你有用?

import requests
r = requests.get('https://logos-download.com/wp-content/uploads/2016/10/Python_logo_wordmark.png')

if r.status_code == 200:
    choice = input("File available!\nDownload? Y/N: ").capitalize().strip()
    if choice == 'Y':
        with open('aim.png','wb') as f:
            f.write(r.content)
    else:
        print("Good bye!")

类似于下面的内容。有关详细信息,请参阅 HTTP head

import requests
urls = ['https://www.google.com','https://www.google.com/you_can_not_find_me']
for idx,url in enumerate(urls,1):
  r = requests.head(url)
  if r.status_code == 200:
    print(f'{idx}) {url} was found')
  else:
    print(f'{idx}) {url} was NOT found')

输出

1) https://www.google.com was found
2) https://www.google.com/you_can_not_find_me was NOT found