Pythonで存在しているURLを捜索
リドルゲームで完全に詰まった時などにどのURLが存在しているかの一覧が欲しくなったりする。
一つ一つURLを叩いて調べるのは骨が折れるので総当りをして存在するURLを捜索するコードを書いた。
元ネタはこちらを参考に英数混じりの文字列を試すようにした。
#! /usr/bin/python
#-*- using:utf-8 -*-
import urllib2
import sys
def checkURL(url):
try:
f = urllib2.urlopen(url)
output = open("result.txt","a")
output.write(url)
output.write("\n")
output.close()
print " URL Found : " + url
f.close()
except urllib2.HTTPError:
print "NotFound : " + url
except:
pass
if 'unichr' not in globals():
unichr = chr
def moziretu(chars, len):
if len <= 0:
yield ''
else:
for c in chars:
for s in moziretu(chars, len - 1):
yield c + s
chars = [unichr(97 + i) for i in range(0, 26)] + [unichr(48 + i) for i in range(0, 10)]
minlen = 1
maxlen = 8
if __name__ == '__main__':
for l in range(minlen, maxlen + 1):
for thisURL in moziretu(chars, l):
url = "https://www.google.co.jp/" + thisURL
checkURL(url)
url の前半が固定する部分、後半が捜索したい文字列になっている。
minlenとmaxlenで最大最小長さを決められる。
上記を checkURL.py で保存して ./checkURL.py で実行する。
パーミッションの変更を忘れずに*1。
結果は画面に出力されるが、存在していたもののみ一覧になって result.txt に出力される。
ただ、動作がとても遅いので辛抱強く待たなければならないが、あまり高速でも先方のサーバーに負担をかけるので程々が良いだろう。
[MBA] smith: checkURL $ emacs checkURL.py
[MBA] smith: checkURL $ chmod 744 ./checkURL.py
[MBA] smith: checkURL $ ./checkURL.py
URL Found : https://www.google.co.jp/a
NotFound : https://www.google.co.jp/b
NotFound : https://www.google.co.jp/c
NotFound : https://www.google.co.jp/d
NotFound : https://www.google.co.jp/e
NotFound : https://www.google.co.jp/f
NotFound : https://www.google.co.jp/g
NotFound : https://www.google.co.jp/h
NotFound : https://www.google.co.jp/i
NotFound : https://www.google.co.jp/j
NotFound : https://www.google.co.jp/k
NotFound : https://www.google.co.jp/l
URL Found : https://www.google.co.jp/m
NotFound : https://www.google.co.jp/n
NotFound : https://www.google.co.jp/o
NotFound : https://www.google.co.jp/p
NotFound : https://www.google.co.jp/q
NotFound : https://www.google.co.jp/r
NotFound : https://www.google.co.jp/s
NotFound : https://www.google.co.jp/t
NotFound : https://www.google.co.jp/u
NotFound : https://www.google.co.jp/v
NotFound : https://www.google.co.jp/w
NotFound : https://www.google.co.jp/x
^C
[MBA] smith: checkURL $ more result.txt
[MBA] smith: checkURL $