python:基于flask有道智云API在线翻译网页

2023-11-06 23:20

本文主要是介绍python:基于flask有道智云API在线翻译网页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 基于 Flask 的网页翻译系统,用户可以在页面中输入需要翻译的文本,并选择翻译语言,系统将通过有道智云API返回翻译结果。

系统应该支持多种语言的翻译,能够准确地翻译文本,并将翻译结果返回给用户。用户应该能够在界面中看到翻译的结果,并可以进行复制或分享。

index.html

<!DOCTYPE html>
<html>
<head><title>基于flask&amp;有道智云API在线翻译</title><style>body {font-family: Arial, Helvetica, sans-serif;margin: 0;padding: 0;background-color: #f2f2f2;}h1 {text-align: center;margin-top: 50px;font-size: 36px;}form {margin-top: 30px;max-width: 100%;margin-left: auto;margin-right: auto;background-color: #fff;padding: 20px;border-radius: 10px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);}label {font-weight: bold;}textarea {width: 100%;margin-top: 5px;margin-bottom: 15px;border-radius: 5px;padding: 5px;}select {margin-top: 5px;margin-bottom: 15px;border-radius: 5px;padding: 5px;}input[type="submit"] {display: block;margin-top: 20px;margin-left: auto;margin-right: auto;padding: 10px 20px;font-size: 18px;font-weight: bold;text-transform: uppercase;color: #fff;background-color: #4CAF50;border-radius: 5px;border: none;cursor: pointer;transition: all 0.3s ease;}input[type="submit"]:hover {background-color: #3E8E41;}h2 {margin-top: 30px;font-size: 24px;}p {margin-top: 10px;font-size: 18px;font-weight: bold;background-color: #D5F5E3;padding: 10px;border-radius: 5px;border: 1px solid #4CAF50;}m {margin-top: 10px;font-size: 18px;font-weight: bold;background-color: #f5f5f5;padding: 10px;border-radius: 5px;}</style>
</head>
<body><h1>基于flask&amp;有道智云API在线翻译</h1><form method="post" action="/"><label for="input_text">需要翻译的文本:</label><br><textarea id="input_text" name="input_text" rows="5" cols="40">{% if input_text %}{{ input_text }}{% endif %}</textarea><br><label for="target_language">需要翻译的语言:</label><br><select id="target_language" name="target_language"><option value="zh-CHS" {% if target_language == "zh-CHS" %}selected{% endif %}>Chinese Simplified</option><option value="zh-CHT" {% if target_language == "zh-CHT" %}selected{% endif %}>Chinese Traditional</option><option value="en" {% if target_language == "en" %}selected{% endif %}>English</option><option value="ja" {% if target_language == "ja" %}selected{% endif %}>Japanese</option><option value="ko" {% if target_language == "ko" %}selected{% endif %}>Korean</option><option value="fr" {% if target_language == "fr" %}selected{% endif %}>French</option><option value="es" {% if target_language == "es" %}selected{% endif %}>Spanish</option><option value="pt" {% if target_language == "pt" %}selected{% endif %}>Portuguese</option><option value="it" {% if target_language == "it" %}selected{% endif %}>Italian</option><option value="ru" {% if target_language == "ru" %}selected{% endif %}>Russian</option><option value="vi" {% if target_language == "vi" %}selected{% endif %}>Vietnamese</option><option value="de" {% if target_language == "de" %}selected{% endif %}>German</option><option value="ar" {% if target_language == "ar" %}selected{% endif %}>Arabic</option><option value="id" {% if target_language == "id" %}selected{% endif %}>Indonesian</option><option value="af" {% if target_language == "af" %}selected{% endif %}>Afrikaans</option><option value="bs" {% if target_language == "bs" %}selected{% endif %}>Bosnian</option><option value="bg" {% if target_language == "bg" %}selected{% endif %}>Bulgarian</option><option value="yue" {% if target_language == "yue" %}selected{% endif %}>Cantonese</option><option value="ca" {% if target_language == "ca" %}selected{% endif %}>Catalan</option><option value="hr" {% if target_language == "hr" %}selected{% endif %}>Croatian</option><option value="cs" {% if target_language == "cs" %}selected{% endif %}>Czech</option><option value="da" {% if target_language == "da" %}selected{% endif %}>Danish</option><option value="nl" {% if target_language == "nl" %}selected{% endif %}>Dutch</option><option value="et" {% if target_language == "et" %}selected{% endif %}>Estonian</option><option value="fj" {% if target_language == "fj" %}selected{% endif %}>Fijian</option><option value="fi" {% if target_language == "fi" %}selected{% endif %}>Finnish</option><option value="el" {% if target_language == "el" %}selected{% endif %}>Greek</option><option value="ht" {% if target_language == "ht" %}selected{% endif %}>Haitian Creole</option><option value="he" {% if target_language == "he" %}selected{% endif %}>Hebrew</option><option value="hi" {% if target_language == "hi" %}selected{% endif %}>Hindi</option><option value="mww" {% if target_language == "mww" %}selected{% endif %}>Hmong Daw</option><option value="hu" {% if target_language == "hu" %}selected{% endif %}>Hungarian</option><option value="sw" {% if target_language == "sw" %}selected{% endif %}>Swahili</option><option value="tlh" {% if target_language == "tlh" %}selected{% endif %}>Klingon</option><option value="lv" {% if target_language == "lv" %}selected{% endif %}>Latvian</option><option value="lt" {% if target_language == "lt" %}selected{% endif %}>Lithuanian</option><option value="ms" {% if target_language == "ms" %}selected{% endif %}>Malay</option><option value="mt" {% if target_language == "mt" %}selected{% endif %}>Maltese</option><option value="no" {% if target_language == "no" %}selected{% endif %}>Norwegian</option><option value="fa" {% if target_language == "fa" %}selected{% endif %}>Persian</option><option value="pl" {% if target_language == "pl" %}selected{% endif %}>Polish</option><option value="otq" {% if target_language == "otq" %}selected{% endif %}>Queretaro Otomi</option><option value="ro" {% if target_language == "ro" %}selected{% endif %}>Romanian</option><option value="sr-Cyrl" {% if target_language == "sr-Cyrl" %}selected{% endif %}>Serbian (Cyrillic)</option><option value="sr-Latn" {% if target_language == "sr-Latn" %}selected{% endif %}>Serbian (Latin)</option><option value="sk" {% if target_language == "sk" %}selected{% endif %}>Slovak</option><option value="sl" {% if target_language == "sl" %}selected{% endif %}>Slovenian</option><option value="sv" {% if target_language == "sv" %}selected{% endif %}>Swedish</option><option value="ty" {% if target_language == "ty" %}selected{% endif %}>Tahitian</option><option value="th" {% if target_language == "th" %}selected{% endif %}>Thai</option><option value="to" {% if target_language == "to" %}selected{% endif %}>Tongan</option><option value="tr" {% if target_language == "tr" %}selected{% endif %}>Turkish</option><option value="uk" {% if target_language == "uk" %}selected{% endif %}>Ukrainian</option><option value="ur" {% if target_language == "ur" %}selected{% endif %}>Urdu</option><option value="cy" {% if target_language == "cy" %}selected{% endif %}>Welsh</option><option value="yua" {% if target_language == "yua" %}selected{% endif %}>Yucatec Maya</option><option value="sq" {% if target_language == "sq" %}selected{% endif %}>Albanian</option><option value="am" {% if target_language == "am" %}selected{% endif %}>Amharic</option><option value="hy" {% if target_language == "hy" %}selected{% endif %}>Armenian</option><option value="az" {% if target_language == "az" %}selected{% endif %}>Azerbaijani</option><option value="bn" {% if target_language == "bn" %}selected{% endif %}>Bengali</option><option value="eu" {% if target_language == "eu" %}selected{% endif %}>Basque</option><option value="be" {% if target_language == "be" %}selected{% endif %}>Belarusian</option><option value="ceb" {% if target_language == "ceb" %}selected{% endif %}>Cebuano</option><option value="co" {% if target_language == "co" %}selected{% endif %}>Corsican</option><option value="eo" {% if target_language == "eo" %}selected{% endif %}>Esperanto</option><option value="tl" {% if target_language == "tl" %}selected{% endif %}>Filipino</option><option value="fy" {% if target_language == "fy" %}selected{% endif %}>Frisian</option><option value="gl" {% if target_language == "gl" %}selected{% endif %}>Galician</option><option value="ka" {% if target_language == "ka" %}selected{% endif %}>Georgian</option><option value="gu" {% if target_language == "gu" %}selected{% endif %}>Gujarati</option><option value="ha" {% if target_language == "ha" %}selected{% endif %}>Hausa</option><option value="haw" {% if target_language == "haw" %}selected{% endif %}>Hawaiian</option><option value="is" {% if target_language == "is" %}selected{% endif %}>Icelandic</option><option value="ig" {% if target_language == "ig" %}selected{% endif %}>Igbo</option><option value="ga" {% if target_language == "ga" %}selected{% endif %}>Irish</option><option value="jw" {% if target_language == "jw" %}selected{% endif %}>Javanese</option><option value="kn" {% if target_language == "kn" %}selected{% endif %}>Kannada</option><option value="kk" {% if target_language == "kk" %}selected{% endif %}>Kazakh</option><option value="km" {% if target_language == "km" %}selected{% endif %}>Khmer</option><option value="ku" {% if target_language == "ku" %}selected{% endif %}>Kurdish</option><option value="ky" {% if target_language == "ky" %}selected{% endif %}>Kyrgyz</option><option value="lo" {% if target_language == "lo" %}selected{% endif %}>Lao</option><option value="la" {% if target_language == "la" %}selected{% endif %}>Latin</option><option value="lb" {% if target_language == "lb" %}selected{% endif %}>Luxembourgish</option><option value="mk" {% if target_language == "mk" %}selected{% endif %}>Macedonian</option><option value="mg" {% if target_language == "mg" %}selected{% endif %}>Malagasy</option><option value="ml" {% if target_language == "ml" %}selected{% endif %}>Malayalam</option><option value="mi" {% if target_language == "mi" %}selected{% endif %}>Maori</option><option value="mr" {% if target_language == "mr" %}selected{% endif %}>Marathi</option><option value="mn" {% if target_language == "mn" %}selected{% endif %}>Mongolian</option><option value="my" {% if target_language == "my" %}selected{% endif %}>Burmese</option><option value="ne" {% if target_language == "ne" %}selected{% endif %}>Nepali</option><option value="ny" {% if target_language == "ny" %}selected{% endif %}>Chichewa</option><option value="ps" {% if target_language == "ps" %}selected{% endif %}>Pashto</option><option value="pa" {% if target_language == "pa" %}selected{% endif %}>Punjabi</option><option value="sm" {% if target_language == "sm" %}selected{% endif %}>Samoan</option><option value="gd" {% if target_language == "gd" %}selected{% endif %}>Scots Gaelic</option><option value="st" {% if target_language == "st" %}selected{% endif %}>Sesotho</option><option value="sn" {% if target_language == "sn" %}selected{% endif %}>Shona</option><option value="sd" {% if target_language == "sd" %}selected{% endif %}>Sindhi</option><option value="si" {% if target_language == "si" %}selected{% endif %}>Sinhala</option><option value="so" {% if target_language == "so" %}selected{% endif %}>Somali</option><option value="su" {% if target_language == "su" %}selected{% endif %}>Sundanese</option><option value="tg" {% if target_language == "tg" %}selected{% endif %}>Tajik</option><option value="ta" {% if target_language == "ta" %}selected{% endif %}>Tamil</option><option value="te" {% if target_language == "te" %}selected{% endif %}>Telugu</option><option value="auto" {% if target_language == "auto" %}selected{% endif %}>	auto</option></select><br><input type="submit" value="翻译"></form>{% if translated_text %}<div class="out"><m>原始文本:</m><p>{{ input_text }}</p><m>翻译语言:</m><p>{{ target_language }}</p><m>翻译结果:</m><p>{{ translated_text }}</p></div>{% endif %}
</body>
</html>

main.py
 

from flask import Flask, render_template, request
import requests
import json
import hashlib
import random
import timeapp = Flask(__name__)
app.config['SECRET_KEY'] = 'dev'# 有道翻译 API 服务器端点,需要先申请App Key和App Secret
api_endpoint = "https://openapi.youdao.com/api"
app_key = "自己获取"
app_secret = "自己获取"def generate_sign_params(query):"""使用app_key和app_secret生成有道翻译API请求参数中的sign参数"""salt = str(random.randint(1, 65536))sign_param = app_key + query + salt + app_secretmd5 = hashlib.md5()md5.update(sign_param.encode('utf-8'))sign = md5.hexdigest()return {'sign': sign, 'salt': salt}@app.route('/', methods=['GET', 'POST'])
def index():if request.method == 'POST':# 处理表单提交请求input_text = request.form['input_text']target_language = request.form['target_language']# 检测输入语言detect_params = generate_sign_params(input_text)detect_params.update({'q': input_text,'from': 'auto','to': 'auto','appKey': app_key})detect_response = requests.get(api_endpoint, params=detect_params)if detect_response.status_code != 200:# 如果响应状态码不是200,则说明请求出错return 'Error: Could not retrieve language.'detected_language = detect_response.json().get('lang')print(str(detected_language))# 调用翻译 API 进行翻译translate_params = generate_sign_params(input_text)translate_params.update({'q': input_text,'from': detected_language,'to': target_language,'appKey': app_key})translate_response = requests.get(api_endpoint, params=translate_params)if translate_response.status_code != 200:# 如果响应状态码不是200,则说明请求出错return 'Error: Could not translate the text.'# 处理翻译结果并返回translated_text = translate_response.json().get('translation')[0]return render_template('index.html', input_text=input_text,target_language=target_language,translated_text=translated_text)# 渲染首页return render_template('index.html')if __name__ == '__main__':app.run(debug=True)

这篇关于python:基于flask有道智云API在线翻译网页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/359669

相关文章

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.