Skip to content

Instantly share code, notes, and snippets.

@fedir
Last active May 25, 2022 14:56
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fedir/5883651 to your computer and use it in GitHub Desktop.
Save fedir/5883651 to your computer and use it in GitHub Desktop.
Check if URL exists via Python
from urllib2 import urlopen
code = urlopen("http://example.com/").code
if (code / 100 >= 4):
print "Nothing there."
# via http://stackoverflow.com/questions/1966086/how-can-i-determine-if-anything-at-the-given-url-does-exist
import urllib2
ret = urllib2.urlopen('http://hostname/directory/file.jpg')
if ret.code == 200:
print "Exists!"
# via http://stackoverflow.com/questions/7347888/how-do-i-check-if-a-file-on-http-exists-using-python-django
@FlaminHat
Copy link

FlaminHat commented Apr 29, 2020

ImportError: No module named 'urllib2'

It reckons there is no module named 'urllib2' and "pip install urllib2" doesn't work either.

pip install urllib2
Collecting urllib2
ERROR: Could not find a version that satisfies the requirement urllib2 (from versions: none)
ERROR: No matching distribution found for urllib2

@meryemCH
Copy link

Hello, i wanna fetch an url in server with authentication but i never succeeded can u help me please

@fedir
Copy link
Author

fedir commented May 24, 2022

Hi @FlaminHat @meryemCH instead of using:

import urllib2

use code below in python3

import urllib.request as urllib2

@meryemCH
Copy link

meryemCH commented May 25, 2022

thank you for answering! @fedir
i need your help please :)
actually,i have a file list json like this : file_name_list = { "itus": [ "artist", "artist_tion", "asong", "arti_type", "colln", "collen_song", "colln_type", "genre", "nre_arst", "nre_collion", "role", "song", "storefront", "media_type", ], "moch": ["arst_mh", "con_mch", "so_mh"], "pority": ["al_parity_per_gre", "sg_polarity_per_genre"], }
in the total i have about 19 files i wanna check if this list of files exist ,the name of file should be like this : itus/artist , itus/artist_tion etc for all list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment