Python 实现获取随机单词翻译

from random_words import RandomWords
import requests
import json

rw = RandomWords()

YOUDAO_URI = 'http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i='
MAX_WORD_LETTER = 20
WORD_NUMBER = 10

vocabulary = rw.random_words(count = WORD_NUMBER)

for i in range(len(vocabulary)):
	url = YOUDAO_URI + vocabulary[i]

	result = requests.get(url)
	detail = json.loads(result.content)
	print(vocabulary[i], end = '')

	if len(vocabulary[i]) <= MAX_WORD_LETTER:
		for x in range(MAX_WORD_LETTER - len(vocabulary[i])):
			print(" ", end='')
	
	print(detail['translateResult'][0][0]['tgt'])

Python 实现获取随机单词翻译_第1张图片

你可能感兴趣的:(Python)