I’ve proceeded with my project further. Now I’m almost done but I’m not getting the header and word type, be it substantive or adjective etc. Where is the problem ?
I have just 2 files,app.py and template/index.html. I’m getting this
instead of this:
So the příslovce word is missing
app.py
from flask import Flask, render_template, request, session
import requests
from bs4 import BeautifulSoup
import json
app = Flask(__name__)
app.secret_key = 'your_secret_key'
@app.route('/', methods=['GET', 'POST'])
def index():
if 'en_cz_translations' not in session:
session['en_cz_translations'] = []
if 'cz_en_translations' not in session:
session['cz_en_translations'] = []
if request.method == 'POST':
term = request.form.get('term')
direction = request.form.get('direction')
if direction == 'en_cz':
url = f'https://slovnik.seznam.cz/preklad/anglicky_cesky/{term}'
elif direction == 'cz_en':
url = f'https://slovnik.seznam.cz/preklad/cesky_anglicky/{term}'
else:
return render_template('index.html', en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'lxml')
# Najdeme skript obsahující JSON data
script_tag = soup.find('script', {'id': '__NEXT_DATA__'})
if script_tag:
try:
json_data = json.loads(script_tag.string)
# Extrahujeme relevantní část JSON dat
translations = json_data['props']['pageProps']['translations']
term_data = []
for translation in translations:
sens_data = []
for sens in translation['sens']:
desc = sens.get('desc2', '')
note = sens.get('note2', '')
morf = sens.get('morf', '')
samples = [f"{samp['samp2s']} -> {samp['samp2t']}" for samp in sens.get('samp2', [])]
trans = [''.join(t) for t in sens.get('trans', []) if t]
coll2 = [f"{c['coll2s']} -> {c['coll2t']}" for c in sens.get('coll2', [])]
sens_data.append({'desc': desc, 'note': note, 'morf': morf, 'samples': samples, 'trans': trans, 'coll2': coll2})
term_data.append(sens_data)
if direction == 'en_cz':
session['en_cz_translations'] = [(term, term_data)] + session['en_cz_translations']
else:
session['cz_en_translations'] = [(term, term_data)] + session['cz_en_translations']
except json.JSONDecodeError as e:
error = f'Error decoding JSON: {str(e)}'
return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
else:
error = 'Error: Unable to find the JSON data script tag.'
return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
else:
error = f'Error: Unable to fetch data for term "{term}".'
return render_template('index.html', error=error, en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
return render_template('index.html', en_cz_translations=session['en_cz_translations'], cz_en_translations=session['cz_en_translations'])
if __name__ == '__main__':
app.run(debug=True)