import requests
import json
class Dao(object):
def __init__(self, word):
self.url = "https://dict.youdao.com/webtranslate"
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
}
self.post__data = {
"from": "auto",
"to": "auto",
"w": word
}
def get_data(self):
response = requests.post(self.url,headers=self.headers)
return response.content
def parse_data(self,data):
dict_data = json.loads(data)
try:
print(dict_data['content'] ['out'])
except:
print(dict_data['content']['word_mean'][0])
def run(self):
data = self.get_data()
self.parse_data(data)
if __name__ == '__main__':
word = input("请输入你要翻译的单词或句子: ")
Dao = Dao(word)
Dao.run()
print(dict_data['content']['word_mean'][0])
~~~~~~~~~^^^^^^^^^^^
KeyError: 'content'
怎么处理呀