import json
import requests
def translation():
# url
url = 'https://fanyi.baidu.com/sug'
# 动态获取关键字相关内容
kw = input('请输入搜索内容:')
params = {
'kw':kw
}
# ua检测
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.197.400 QQBrowser/11.6.5265.400'
}
#发送请求
respond = requests.post(url=url,data=params,headers=headers)
#对返回的json数据进行解析
dic_obj = respond.json()
fileName = kw +'.json'
fp = open(fileName,'w+',encoding='utf-8')
#json持久化保存数据
json.dump(fp=fp,obj=dic_obj,ensure_ascii=False)
if __name__ == '__main__':
while True:
translation()
answer = input('是否继续翻译?y或n' + '\n')
if answer != 'Y' and answer != 'y':
break
python百度翻译案例
于 2023-03-12 21:36:37 首次发布