Skip to content

Instantly share code, notes, and snippets.

@ompugao
Last active February 20, 2020 10:56
Show Gist options
  • Save ompugao/614a0d20c195dc79c7241c360e615f4d to your computer and use it in GitHub Desktop.
Save ompugao/614a0d20c195dc79c7241c360e615f4d to your computer and use it in GitHub Desktop.
LLN user dictionary to save words you look into
#!/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()

run $ pip install flask requests beautifulsoup4

and then set http://localhost:5000/WORD to user dictionary url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment