こんにちは、テリーです。普段ウェブで英語の文章を読むときにはweblioポップアップ英単語機能をよく使っています。調べた単語を単語帳に記録してあとでまとめてエクスポートできるんですけど、和訳は一緒にエクスポートできないんですよね。なのでweblioの単語帳をベースに英単語の和訳をスクレイピングで取得してみました。ついでに発音記号も!
スポンサーリンク
ソースコード
Pythonで以下の通りやってみました。アウトプットは最終的にCSV出力とする予定ですが、とりあえず確認用に画面出力されるところまで。
import urllib.request from bs4 import BeautifulSoup import time #レベル定義 vocab_level = ['0', '1語~500語', '1001語~1500語', '1001語~1500語', '1501語~2000語', '2001語~2500語', '2501語~3000語', '3001語~3500語', '3501語~4000語', '4001語~4500語', '4501語~5000語', '5001語~5500語', '5501語~6000語', '6001語~7000語', '7001語~8000語', '8001語~9000語', '9001語~10000語', '10001~11000語', '11001~12000語', '12001~13000語', '13001~14000語', '14001~15000語', '15001~16000語', '16001~17000語', '17001~18000', '18001~20000語', '20001~22000語', '22001~24000語', '24001~26000語', '26001~28000語'] fqdn = 'https://ejje.weblio.jp/content/' with open('./english/word-list.txt', 'r') as f: words = f.readlines() for word in words: word = word.replace('\n','') word = word.replace('\r','') path_word = word.replace(" ","+") response = urllib.request.urlopen(fqdn + path_word) data = response.read() soup_word = BeautifulSoup(data, "lxml") mean = soup_word.find("td", class_="content-explanation").string pronounce = soup_word.find("span", class_="phoneticEjjeDesc")#.string country = soup_word.find("span", class_="phoneticEjjeDc")#.string level = soup_word.find("span", class_="learning-level-content")#.string print(word) if None is not pronounce and None is not country : print(pronounce.string + ' : ' + country.string) elif None is not pronounce : print(pronounce.string) else : print(pronounce) print(mean) if None is not level : print("Level:", level.string + ' (' + vocab_level[int(level.string)] + ')') print("------------") # スクレイピングマナー time.sleep(5)
実行結果
実行結果は以下の通りで、上から調べたい単語、発音記号(一応国も)、主な和訳、単語レベルという順で表示するようにしています。
snippet snípɪt : (米国英語) 切れ端、断片、少し、わずか、取るに足らない人 Level: 16 (9001語~10000語) ------------ tuck away None (安全な場所などに)しまい込む、隠す、たくさん食べる、建てる Level: 26 (20001~22000語) ------------ majorly None かなり;たいてい Level: 22 (15001~16000語) ------------ schematically None 模式的に Level: 18 (11001~12000語) ------------ annotation `ænətéɪʃən : (米国英語) 記注、注釈 Level: 16 (9001語~10000語) ------------ recap rìːkˈæp : (米国英語) 再生する Level: 15 (8001語~9000語)
今後やること
今は表示するだけですが、時間があるときにCSVファイル等に出力し、そのままAnkiアプリに入れられるように整えたいと思います。
完成次第この記事をアップデートします。