forked from jeffjxu/blue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordRef.py
36 lines (26 loc) · 860 Bytes
/
WordRef.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import flask
from flask import request, jsonify
from urllib.parse import quote
import requests
from bs4 import BeautifulSoup
app = flask.Flask(__name__)
app.config["DEBUG"] = True
URL = 'https://www.wordreference.com/fren/'
@app.route('/', methods=['GET'])
def home():
return ''
@app.route('/api/resources/wordref', methods=['GET'])
def api_wordref():
if 'word' in request.args:
word = request.args['word']
else:
return "Error: No word field provided. Please specify an word."
req = URL + quote(word)
print(req)
resp = requests.get(req)
soup = BeautifulSoup(resp.content, features='lxml')
content = soup.find("table", {'class': 'WRD'})
content = str(content).replace('vtr', 'vtr ').replace('vi', 'vi ').replace('npl', 'npl ')
content = content.replace('nf', 'nf ')
return content
app.run()