Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

rest

http

I have to check whether url exists or not using python. I am trying to use requests.get(url) but it is taking alot of time as the file starts downloading as soon as get is hit. I don't want the file to be downloaded for checking the url validity. Can this be achieved using python ?

like image 442
Barsha Mandal Avatar asked Oct 22 '25 17:10

Barsha Mandal


1 Answers

Something like the below. See HTTP head for more info.

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')

output

1) https://www.google.com was found
2) https://www.google.com/you_can_not_find_me was NOT found
like image 110
balderman Avatar answered Oct 25 '25 07:10

balderman