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开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑