SoFunction
Updated on 2024-11-13

python Simple Calls to Youdao Translator

coding

import json

import requests

# Translation function, word What needs to be translated
def translate(word):
  # Wealth Dictionary api
  url = '/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
  # Parameters to be transmitted, where i is the content to be translated
  key = {
    'type': "AUTO",
    'i': word,
    "doctype": "json",
    "version": "2.1",
    "keyfrom": "",
    "ue": "UTF-8",
    "action": "FY_BY_CLICKBUTTON",
    "typoResult": "true"
  }
  # key This dictionary is the content that is sent to the Youtube server.
  response = (url, data=key)
  # Determine if the server responded successfully
  if response.status_code == 200:
    # And then the corresponding results
    return 
  else:
    print("Arigatou Dictionary call failed.")
    # Returns null if the correspondence fails
    return None

def get_reuslt(repsonse):
  # by loading the returned results into json format
  result = (repsonse)

  return result['translateResult'][0][0]['tgt']

def main(err):
  word = err
  list_trans = translate(word)
  return get_reuslt(list_trans)

print(main('Fish'))

"""
"""

Running effects:

The above is python simple call have a way to translate the details, more about python call have a way to translate the information please pay attention to my other related articles!