黑松山资源网 Design By www.paidiu.com

一、网页分析
打开谷歌翻译链接:https://translate.google.com/

按F12,点击network。在左侧输入"who are you"

python 调用Google翻译接口的方法

可以看到,请求的链接为:

https://translate.google.com/_/TranslateWebserverUi/data/batchexecute"text-align: center">python 调用Google翻译接口的方法

这里面的who are you表示,需要翻译的文字

ja 表示日本的简称。

二、代码演示

# !/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import re


def translated_content(text, target_language):
  headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    # "accept-language": "en,zh-CN;q=0.9,zh;q=0.8",
    "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36"
  }
  # 请求url
  url = "https://translate.google.com/_/TranslateWebserverUi/data/batchexecute"
  # 数据参数
  from_data = {
    "f.req": r"""[[["MkEWBc","[[\"{}\",\"auto\",\"{}\",true],[null]]",null,"generic"]]]""".format(text, target_language)
  }
  try:
    r = requests.post(url, headers=headers, data=from_data, timeout=60)
    if r.status_code == 200:
      # 正则匹配结果
      response = re.findall(r',\[\[\\"(.*",\[\\', r.text)
      if response:
        response = response[0]
      else:
        response = re.findall(r',\[\[\\"(.*"]', r.text)
        if response:
          response = response[0]
      return response
  except Exception as e:
    print(e)
    return False

# 翻译各个国家语言
for i in ['en', 'zh', 'fr', 'ja', 'de']:
  response = translated_content("who are you", i)
  print(response)

执行输出:

python 调用Google翻译接口的方法

以上就是python 调用Google翻译接口的方法的详细内容,更多关于python 调用Google翻译接口的资料请关注其它相关文章!

黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。