Check if the URL exists in Python

Introduction

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.

Sample code

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 aHTTPError`.

Recommended Posts

Check if the URL exists in Python
[python] How to check if the Key exists in the dictionary
Check if the expected column exists in Pandas DataFrame
Check the behavior of destructor in Python
Check if the password hash generated by PHP matches in Python
Create Python folder Check if it already exists
Issue the Amazon CloudFront Signed URL in Python
In bash, "Delete the file if it exists".
Download the file in Python
Find the difference in Python
Crawl the URL contained in the twitter tweet with python
Check if it is Unix in the scripting language
Check if it is Unix in the scripting language
Get the URL of the HTTP redirect destination in Python
Relative url handling in python
Check the asymptotic nature of the probability distribution in Python
[Python] Check the installed libraries
Create a python script to check if the link at the specified URL is valid 2
Create a python script to check if the link at the specified URL is valid
How to check in Python if one of the elements of a list is in another list
Check the operation of Python for .NET in each environment
Master the type in Python? (When should type check be done)
How to check if a value exists in an enum
Check for memory leaks in Python
[Python] Check the current directory, move the directory
Getting the arXiv API in Python
Check for external commands in python
Check the behavior when assigning Python
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Create python directory Support if directory exists
Check and move directories in Python
Hit the web API in Python
Check the data summary in CASTable
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python
Access the Twitter API in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
How to check if the contents of the dictionary are the same in Python by hash value
python Note: Determine if command line arguments are in the list
Check if the configuration file is read in an easy-to-understand manner
How to check the memory size of a variable in Python
Delete a particular character in Python if it is the last
How to check the memory size of a dictionary in Python
Check if you can connect to a TCP port in Python
Have python check if the string can be converted / converted to int
[Golang] Check if a specific character string is included in the character string
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Load the remote Python SDK in IntelliJ
Try using the Wunderlist API in Python
Learn the design pattern "Flyweight" in Python
Try using the Kraken API in Python