生信人

找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

0

收听

12

听众

278

主题
发表于 2024-1-16 11:15:35 | 查看: 1020| 回复: 0

背景

初次api使用实例,找了一些教程,最后跑通了一次翻译调用。

方便大家学习,代码分享如下:

使用的是pycharm社区版

代码

tips:其中appid和appkey是作为使用百度通用翻译的凭据,需要自己注册申请,百度翻译开放平台

import requests
import random
from hashlib import md5


# GET 请求
def trans_baidu_get(query, appid, appkey, fromlanguage='auto', tolanguage='en', action=0):
    """
    query: 待翻译内容
    appid: 申请的 APP ID
    appkey: 申请的密钥
    fromlanguage: 待翻译语言; 'auto' 表示自动识别
    tolanguage: 翻译目标语言; 'zh' 表示中文       * 语言代码见: https://api.fanyi.baidu.com/doc/21
    action: 1: 使用自定义术语干预API; 0: 不使用自定义术语干预API
    """

    # 定义函数,作用是进行 MD5 并将散列值转换为 16 进制
    def make_md5(s, encoding='utf-8'):
        return md5(s.encode(encoding)).hexdigest()
    # 在 32768 至 65536 的范围内取随机数


    salt = random.randint(32768, 65536)
    # 使用 appid、请求 query、随机数和密钥构成签名
    sign = make_md5(appid + query + str(salt) + appkey)

    # 发送请求的 URL
    url = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
    # 以字典形式编辑查询参数
    parameters = {'appid': appid, 'q': query, 'from': fromlanguage, 'to': tolanguage, 'salt': salt, 'sign': sign}
    # 返回响应信息,并提取响应中的翻译结果
    response = requests.get(url, params=parameters)
    result_list = response.json()['trans_result']
    # 提取翻译结果中的翻译后内容 (dst)
    result = '\n'.join(item['dst'] for item in result_list)
    return result


# POST 请求
def trans_baidu_post(query, appid, appkey, fromlanguage='auto', tolanguage='en', action=0):
    # 定义函数,作用是进行 MD5 并将散列值转换为 16 进制
    def make_md5(s, encoding='utf-8'):
        return md5(s.encode(encoding)).hexdigest()

    salt = random.randint(32768, 65536)
    sign = make_md5(appid + query + str(salt) + appkey)
    # 发送通用翻译请求的 URL
    url = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
    # 根据 API 接入文档,指定 Content-Type 为 application/x-www-form-urlencoded
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    parameters = {'appid': appid, 'q': query, 'from': fromlanguage, 'to': tolanguage, 'salt': salt, 'sign': sign}
    # 保存响应,并提取响应中的翻译结果
    response = requests.post(url, params=parameters, headers=headers)
    result_list = response.json()['trans_result']
    # 提取翻译结果中的翻译后内容 (dst)
    result = '\n'.join(item['dst'] for item in result_list)
    return result


appid = '' #自己申请后填入
appkey = '' #自己申请后填入

# GET 请求
answer1 = trans_baidu_get('成为生信人论坛会员,用最独家的数据,学最实用的Python,画最酷的图!', appid, appkey)
print(answer1)

'''
Become a member of the Shengxin Forum, use the most exclusive data, learn the most practical Python, and draw the coolest pictures!
'''

# POST 请求
answer2 = trans_baidu_post('成为生信人论坛会员,\n用最独家的数据,\n学最实用的Python,\n画最酷的图!', appid, appkey)
print(answer2)

'''
Becoming a member of the Biotrust Forum,
Using the most exclusive data,
Learn the most practical Python,
Draw the coolest picture!
'''  

收藏回复 显示全部楼层 道具 举报

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|生信人

GMT+8, 2024-4-29 15:13 , Processed in 0.049726 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表