Pythonでpythonwhoisを用いてWHOISを検索してみます。
■Python

今回のPythonのバージョンは、「3.8.5」を使用しています。(Windows10)(pythonランチャーでの確認)
■pythonwhoisを用いてWHOISを検索する
では、早速pythonwhoisを用いてWHOISを検索し情報を出力するスクリプトを書いていきます。
■コード
import pythonwhois domain = "****.***(ドメイン名)" print(pythonwhois.get_whois(domain))
importでpythonwhoisモジュールを呼び出します。その後、domainという変数を定義し、その中にWHOIS情報を検索したいドメインを格納します。格納後、pythonwhois.get_whois()を用います。括弧内には引数,パラメータとしてdomain変数を渡します。これでWHOIS情報を取得し、情報が返されます。返された情報をprint()で出力します。なお、今回はdomain変数内には当サイトのドメインを格納しています。
■実行・検証
このスクリプトを「py_w.py」という名前で、Pythonが実行されている作業ディレクトリ(カレントディレクトリ)に保存し、コマンドプロンプトから実行してみます。
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\sre_parse.py", line 1039, in parse_template
this = chr(ESCAPES[this][1])
KeyError: '\\s'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "py_w.py", line 1, in
import pythonwhois
File "C:\Users\user_\AppData\Roaming\Python\Python38\site-packages\pythonwhois\__init__.py", line 1, in
from . import net, parse
File "C:\Users\user_\AppData\Roaming\Python\Python38\site-packages\pythonwhois\parse.py", line 363, in
registrant_regexes = [preprocess_regex(regex) for regex in registrant_regexes]
File "C:\Users\user_\AppData\Roaming\Python\Python38\site-packages\pythonwhois\parse.py", line 363, in
registrant_regexes = [preprocess_regex(regex) for regex in registrant_regexes]
File "C:\Users\user_\AppData\Roaming\Python\Python38\site-packages\pythonwhois\parse.py", line 205, in preprocess_regex
regex = re.sub(r"\\s\*\(\?P<([^>]+)>\.\+\)", r"\s*(?P<>\S.*)", regex)
File "C:\Program Files\Python38\lib\re.py", line 210, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "C:\Program Files\Python38\lib\re.py", line 327, in _subx
template = _compile_repl(template, pattern)
File "C:\Program Files\Python38\lib\re.py", line 318, in _compile_repl
return sre_parse.parse_template(repl, pattern)
File "C:\Program Files\Python38\lib\sre_parse.py", line 1042, in parse_template
raise s.error('bad escape %s' % this, len(this))
re.error: bad escape \s at position 0
実行してみると、re.errorというエラーが出力されました。エラーの原因を調べて修正を行います。
まずは問題が発生しているスクリプトファイルである「C:\Users\(ユーザー名)\AppData\Roaming\Python\(Pythonのバージョン)\site-packages\pythonwhois\parse.py」をコードエディタで開きます。
■コード(修正前)
regex = re.sub(r"\\s\*\(\?P<([^>]+)>\.\+\)", r"\s*(?P<>\S.*)", regex)
■コード(修正後)
regex = re.sub(r"\\s\*\(\?P<([^>]+)>\.\+\)", r"\\s*(?P<>\\S.*)", regex)
開いた後に上記のコードを修正します。修正後、スクリプトファイルを保存します。保存後、再度スクリプトを実行してみます。
実行してみると、今回指定したドメインのWHOIS情報を出力させることができました。


コメント