LoginSignup
1
0

More than 1 year has passed since last update.

「英語→カタカナ変換機」を叩いて英単語をカタカナに変換したい

Last updated at Posted at 2021-10-07

何?

英語→カタカナ変換機をBashかPythonで叩きたいなあと思った。

注意

連続して取得したい場合は十分なウェイトを空けておくこと。

Bash

word="test"
curl 'https://www.sljfaq.org/cgi/e2k_ja.cgi?o=json&word='"${word}"'&lang=ja' \
     -A 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
{
  "words": [
    {
      "j_pron_spell": "テスト",
      "type": "dictionary",
      "word": "test",
      "j_pron_only": "テスト"
    }
  ],
  "is_romaji": 0,
  "has_spelling": 0
}

Python

from json import loads
from json import loads
from pprint import pp
from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = "https://www.sljfaq.org/cgi/e2k_ja.cgi"
data = {
    "o": "json",
    "word": "test",
    "lang": "ja",
}
headers = {
    "User-Agent": (
        "Mozilla/5.0 (X11; Linux x86_64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/51.0.2704.103 Safari/537.36"
    ),
}
params = urlencode(data, doseq=True)
req = Request(f"{url}?{params}", headers=headers)
res = loads(urlopen(req).read())
pp(res)
{'has_spelling': 0,
 'words': [{'j_pron_spell': 'テスト',
            'type': 'dictionary',
            'word': 'test',
            'j_pron_only': 'テスト'}],
 'is_romaji': 0}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0