It is a memorandum because I created a python script that checks whether the link destination of the specified URL is valid
I referred to the following. (Partially modified for python3.0)
sample.py
#-*- using:utf-8 -*-
import urllib.request, urllib.error
def checkURL(url):
try:
f = urllib.request.urlopen(url)
print ("OK:" + url )
f.close()
except:
print ("NotFound:" + url)
if __name__ == '__main__':
url = "http://qiita.com/"
checkURL(url)
Execution result
$ python sample.py
OK:http://qiita.com/
$ python sample.py
NotFound:http://nonenonenonenone.com
Just enter a multi-line URL from the file
sample2.py
#-*- using:utf-8 -*-
import urllib.request, urllib.error
def checkURL(url):
try:
f = urllib.request.urlopen(url)
print ("OK:" + url )
f.close()
except:
print ("NotFound:" + url)
if __name__ == '__main__':
with open("./input.txt") as f:
for line in f:
# print(line, end='')
checkURL(line)
Execution result
$ cat input.txt
http://qiita.com/
$ python sample2.py
OK:http://qiita.com/
ImportError: No module named 'urllib2'
About the following
import urllib2
In python3.0 series, it is necessary to write as follows.
import urllib.request, urllib.error
[Python / urllib] Check if the URL exists How to check if the file exists at the URL specified in Python Script to check the existence and existence of URL in Python Check if the URL exists in Python Please tell me how to use Module urllib2 of python Read line by line from file with Python
Recommended Posts