run
$ pip install flask requests beautifulsoup4
and then
set http://localhost:5000/WORD
to user dictionary url
#!/usr/bin/env python | |
import requests | |
import json | |
import os | |
from bs4 import BeautifulSoup | |
from flask import Flask | |
app = Flask(__name__) | |
wordfile = 'words.txt' | |
@app.route('/<word>') | |
def main(word): | |
my_header = { | |
"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NP06; rv:11.0) like Gecko" | |
} | |
data = requests.get('https://ejje.weblio.jp/content/{}'.format(word), headers = my_header) | |
data.encoding = data.apparent_encoding | |
data = data.text | |
soup = BeautifulSoup(data, "html.parser") | |
p = soup.select_one('#summary > div.summaryM.descriptionWrp > table > tbody > tr > td.content-explanation.ej') | |
if p is None or not hasattr(p, 'text'): | |
return 'not found in weblio' | |
with open(wordfile, 'a') as f: | |
f.write(word + ' : ' + p.text + '\n') | |
return p.text | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0') | |
#app.run() | |