Recently, I wanted to make a program to check if the URL exists, so I couldn't find the sample code to check if the URL exists in Python at that time, so I recorded it here.
This time, it is a sample code to check if "http://qiita.com/" exists.
confirm_url.py
#-*- using:utf-8 -*-
import urllib2
def checkURL(url):
try:
f = urllib2.urlopen(url)
print url
f.close()
except urllib2.HTTPError:
print "NotFound:" + url
if __name__ == '__main__':
url = "http://qiita.com/"
checkURL(url)
Well, if you look at the code, it's clear that you're not doing anything special ...
I'm just using ʻurllib2to try to connect to a URL, and if I can't connect, I get a
HTTPError`.
Recommended Posts